The following code snippet demonstrates the format modification of date in a python pandas dataframe. My requirement was to convert the date in DD.MM.YYYY format to DD-MM-YYYY format. The code snippet is given below.
import pandas as pd df = pd.DataFrame({'DATE': {0: '16.12.2022', 1: '15.12.2022', 2:'14.12.2022', 3:'13.12.2022'}}) # converting string field to datetime YYYY-MM-DD format df['DATE'] = pd.to_datetime(df.DATE) print(df) # adjusting the structure in DD-MM-YYYY format # The strftime will convert the datetime to unicode. So if you want datetime, convert it back. df = df['DATE'].dt.strftime('%d-%m-%Y') print(df)
I hope this tip is useful.