简体   繁体   中英

Pandas change day/month/year format to month/day/year

I have a data set in a Pandas data frame with dates like this:

  • 2/3/2020
  • 19/3/2020

And I want to have the dates like this:

  • 3/2/2020
  • 3/19/2020

The data set is called COVID and the column is called 'fecha_not'

This is a sample of my data set:

在此处输入图像描述

Hello you can use import datetime and apply it to the column, then with .strftime() you can change the format. Check more here

import datetime

x = datetime.datetime(2020, 3, 19)
x.strftime("%m %d %Y")
'03 19 2020'

Use strftime

In [2]: df = pd.DataFrame({'fecha_not' : ['2/3/2020', '6/3/2020', '7/3/2020', '9/3/2020', '9/3/2020'],
   ...: 'Estodo' : "Leve"})
   ...: df
Out[2]: 
  fecha_not Estodo
0  2/3/2020   Leve
1  6/3/2020   Leve
2  7/3/2020   Leve
3  9/3/2020   Leve
4  9/3/2020   Leve

In [3]: df['fecha_not'] = pd.to_datetime(df.fecha_not).dt.strftime('%d/%m/%Y')
   ...: df
Out[3]: 
    fecha_not Estodo
0  03/02/2020   Leve
1  03/06/2020   Leve
2  03/07/2020   Leve
3  03/09/2020   Leve
4  03/09/2020   Leve

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