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

Time:12-14

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.

CodePudding user response:

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

  • Related