user13930292
user13930292

Reputation:

Python : How to reorder date column in dataframe with pandas dataframe

have a df with values :

name      age 
 
mark    2002-12-19    
tom     2003-11-30

how to reorder the date format with dd mm yyyy

name      age 
 
mark    19-12-2002    
tom     30-11-2003

tried this How to change the datetime format in pandas

but it is storing as string. i need in date format

Upvotes: 0

Views: 272

Answers (2)

Youyoun
Youyoun

Reputation: 338

df.loc[:, "age"] = pd.to_datetime(df["age"], format="%Y-%m-%d").dt.strftime("%d-%m-%Y")

you can then use .sort_values() to sort the dates as you wish.

Upvotes: 0

Chris
Chris

Reputation: 16147

Assuming your date stamps are valid, which your second one appears to not be.

df['age'] = pd.to_datetime(df['age']).dt.strftime('%d-%m-%Y')

Upvotes: 1

Related Questions