user13408251
user13408251

Reputation:

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.

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

Upvotes: 0

Views: 41

Answers (1)

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39820

You can format the date using 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

Upvotes: 2

Related Questions