How to separate dataframe columns as numerical, categorical and datetime?

Question:

I need to split the columns of a dataframe by numerical, categorical, and datatime formats separately. I use df.select_dtypes(include=['int64', 'float64']) and it works, but I’m not sure how to separate the datatime columns?

this is the dataset I have used

enter image description here

this is the command I have used to separate the date time column

date = df.select_dtypes(include=[‘datetime’])

The output I got

enter image description here

please help to solve this.

Asked By: Manoranjini M

||

Answers:

You can use below to separate the datetime columns however this only works if your column if of the datetime data type –

converted_column = df.select_dtypes(include=['datetime'])

If these columns are not in datetime type then we first need to convert and then extract them –

df['Start date'] = pd.to_datetime(df['Start date'])
df['End date'] = pd.to_datetime(df['End date'])

Example output

Answered By: Kartik Shandilya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.