简体   繁体   中英

Converting dataframe column of datetime data to DD/MM/YYYY string data

I have a dataframe column with datetime data in 1980-12-11T00:00:00 format.

I need to convert the whole column to DD/MM/YYY string format.

Is there any easy code for this?

Creating a working example:

df = pd.DataFrame({'date':['1980-12-11T00:00:00', '1990-12-11T00:00:00', '2000-12-11T00:00:00']})
print(df)

                  date
0  1980-12-11T00:00:00
1  1990-12-11T00:00:00
2  2000-12-11T00:00:00

Convert the column to datetime by pd.to_datetime() and invoke strftime()

df['date_new']=pd.to_datetime(df.date).dt.strftime('%d/%m/%Y')
print(df)

                  date    date_new
0  1980-12-11T00:00:00  11/12/1980
1  1990-12-11T00:00:00  11/12/1990
2  2000-12-11T00:00:00  11/12/2000

You can use pd.to_datetime to convert string to datetime data

pd.to_datetime(df['col'])

You can also pass specific format as:

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

When using pandas , try pandas.to_datetime :

import pandas as pd
df = pd.DataFrame({'date': ['1980-12-%sT00:00:00'%i for i in range(10,20)]})
df.date = pd.to_datetime(df.date).dt.strftime("%d/%m/%Y")
print(df)
         date
0  10/12/1980
1  11/12/1980
2  12/12/1980
3  13/12/1980
4  14/12/1980
5  15/12/1980
6  16/12/1980
7  17/12/1980
8  18/12/1980
9  19/12/1980

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM