简体   繁体   中英

how to change dd-mm-yyyy date format to yyyy-dd-mm in pandas

How to change dd-mm-yyyy date format to yyyy-dd-mm in pandas. I have a datefield which is already in dd-mm-yyyy format but when I try

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

it gives output a yyyy-dd-mm

I believe this is what you needed.

import pandas as pd
df = pd.read_csv("dates.csv")

df

   id        date
0   1  25/06/2018
1   2  14-11-2005
2   3  03/10/2010
3   4  13-08-2008
4   5  05-05-2005

Here no need to specify the format as you have tried.

df['date'] =pd.to_datetime(df['date'])
df

 id       date
0   1 2018-06-25
1   2 2005-11-14
2   3 2010-03-10
3   4 2008-08-13
4   5 2005-05-05

Pandas datetime series data do not have an inherent string format.

datetime values are stored internally as integers. For more details, see this answer . String representations are just that, representations . For example, when you use the print command, a specific string representation is used so that data is displayed in a human-readable way.

For most purposes, you should not worry about the representation. If you need a format different to the default representation, ie "YYYY-MM-DD", you can use pd.Series.dt.strftime and specify a string format. For this Python's strftime directives is a useful resource.

Use this:

import pandas as pd

df['date'] = pd.to_datetime(df['date'],format='%d-%m-%Y').dt.strftime('%Y-%m-%d')#specify input format '%d-%m-%Y' and output format '%Y-%m-%d' or change output as desired i.e. %d/%m/%Y to give dd/mm/yyyy

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