简体   繁体   English

Pandas 将日/月/年格式更改为月/日/年

[英]Pandas change day/month/year format to month/day/year

I have a data set in a Pandas data frame with dates like this:我在 Pandas 数据框中有一个数据集,日期如下:

  • 2/3/2020 2020 年 2 月 3 日
  • 19/3/2020 19/3/2020

And I want to have the dates like this:我想要这样的日期:

  • 3/2/2020 2020 年 3 月 2 日
  • 3/19/2020 2020 年 3 月 19 日

The data set is called COVID and the column is called 'fecha_not'数据集称为 COVID,列称为“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.您好,您可以使用 import datetime 并将其应用于列,然后使用.strftime()您可以更改格式。 Check more here在这里查看更多

import datetime

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

Use strftime使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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