Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the format of date in a dataframe? [duplicate]

Tags:

python

pandas

I have a date column in a dataframe in the format yyyy/mm/dd like this:

Date
2016/08/22
2016/08/10
2016/08/08
...

How do I convert it into dd/mm/yyyyformat??

like image 722
user517696 Avatar asked Jan 17 '26 22:01

user517696


2 Answers

I assume the column is in string.

import pandas as pd

df = (pd.DataFrame([['2016/08/22']*10]*10))

df[0] = pd.to_datetime(df[0]).apply(lambda x:x.strftime('%d/%m/%Y'))

output:

enter image description here

like image 94
Alex Fung Avatar answered Jan 19 '26 14:01

Alex Fung


Use a vectorized approached with the dt accessor in combination with strftime

df = pd.DataFrame(dict(date=['2011/11/30', '2012/03/15']))
df['date'] = pd.to_datetime(df.date).dt.strftime('%d/%m/%Y')

print(df)

         date
0  30/11/2011
1  15/03/2012
like image 20
piRSquared Avatar answered Jan 19 '26 15:01

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!