简体   繁体   English

如何更改 pandas 中列的日期格式

[英]How to change the date format of a column in pandas

i just need to make the format of data-frame be like 2020-22-01 not like 2020-01-22 .我只需要将数据框的格式设置为2020-22-01而不是2020-01-22

the code is:代码是:

confirmed_file=pd.read_json('https://api.covid19api.com/country/egypt/status/confirmed/live')
confirmed_file["Date"] = pd.to_datetime(confirmed_cases["Date"]).dt.date

You can format the date using strftime :您可以使用strftime格式化日期:

confirmed_file["Date"] = pd.to_datetime(confirmed_file["Date"]).dt.strftime('%Y-%d-%m')

Full Example:完整示例:

>>> import pandas as pd
>>> 
>>> confirmed_file=pd.read_json('https://api.covid19api.com/country/egypt/status/confirmed/live')
>>> confirmed_file["Date"]
0     2020-01-22 00:00:00+00:00
1     2020-01-23 00:00:00+00:00
2     2020-01-24 00:00:00+00:00
3     2020-01-25 00:00:00+00:00
4     2020-01-26 00:00:00+00:00
                 ...           
475   2021-05-11 00:00:00+00:00
476   2021-05-12 00:00:00+00:00
477   2021-05-13 00:00:00+00:00
478   2021-05-14 00:00:00+00:00
479   2021-05-15 00:00:00+00:00
Name: Date, Length: 480, dtype: datetime64[ns, UTC]
>>> confirmed_file["Date"] = pd.to_datetime(confirmed_file["Date"]).dt.strftime('%Y-%d-%m')
>>> confirmed_file["Date"]
0      2020-22-01
1      2020-23-01
2      2020-24-01
3      2020-25-01
4      2020-26-01
          ...    
475    2021-11-05
476    2021-12-05
477    2021-13-05
478    2021-14-05
479    2021-15-05
Name: Date, Length: 480, dtype: object

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

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