Home > Software engineering >  How to normalise a date columnin pandas dataframe to the same format
How to normalise a date columnin pandas dataframe to the same format

Time:11-26

I have a dataframe made from pulling in different excel sheets.

I am trying to normalise the date_time column to just a standard DD/MM/YYY format. Is that possible?

1 DATE Column 3 Column 4 Column 5 Column 6
2 01/03/2021 00:00
3 01/03/2021 00:00
4 01/03/2021 00:00
5 01/03/2021 00:00
6 01/03/2021 00:00
... ...
122350 11/24/2022
122351 11/24/2022
122352 11/24/2022
122353 11/24/2022
122354 11/24/2022

CodePudding user response:

# example df
df = pd.DataFrame({'DATE': ['01/03/2021 00:00', '01/03/2021 00:00', '01/03/2021 00:00', '01/03/2021 00:00', '01/03/2021 00:00', '11/24/2022', '11/24/2022', '11/24/2022', '11/24/2022', '11/24/2022']})
df['DATE'] = pd.to_datetime(df['DATE'])
df['DATE'] = df['DATE'].dt.strftime('%d/%m/%Y')

output:

> df

         DATE
0  03/01/2021
1  03/01/2021
2  03/01/2021
3  03/01/2021
4  03/01/2021
5  24/11/2022
6  24/11/2022
7  24/11/2022
8  24/11/2022
9  24/11/2022
  • Related