Bimons
Bimons

Reputation: 241

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?

Upvotes: 1

Views: 1437

Answers (3)

anshukira
anshukira

Reputation: 91

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')

Upvotes: 1

anky
anky

Reputation: 75080

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

Upvotes: 1

Chris
Chris

Reputation: 29742

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

Upvotes: 0

Related Questions