Serhii
Serhii

Reputation: 1587

Pandas change date format in a file

df['dates'] =pd.to_datetime(df.dates, format='%d-%m-%Y')
df.sort_values(by='dates', ascending=False).to_csv('test.csv', index=False)

And I look 2017-09-20 in my file, but before run code, it was like 20-09-2017.
I want to use only %d-%m-%Y format.
Thank you.

Upvotes: 1

Views: 107

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

use date_format parameter:

df.to_csv('/path/to/file.csv', date_format='%d-%m-%Y')

Upvotes: 1

Related Questions