Home > Blockchain >  How do I split a string in a pandas dataframe and convert the remaining to datetime format?
How do I split a string in a pandas dataframe and convert the remaining to datetime format?

Time:10-24

I have a column in my pandas dataframe which has rows containing the below:

2021-01-04T23:00:00.000 00:00

I would like to split it and keep only the date element and convert the string to a date format:

04/01/2021

I have tried to use split string but I am unable to get to the final stage of then converting to a date format.

Thank you!

CodePudding user response:

Don't, split. Convert to_datetime and export to the desired format directly with strftime:

df['new'] = pd.to_datetime(df['col']).dt.strftime('%d/%m/%Y')

output:

                             col         new
0  2021-01-04T23:00:00.000 00:00  04/01/2021

CodePudding user response:

convert the date using pd.datatime and then format the date

pd.to_datetime(df['date']).dt.strftime('%d/%m/%Y')
  • Related