joasa
joasa

Reputation: 976

Change date format in pandas dataframe

I have this dataframe:

             date        value
1   Thu 17th Nov 2016   385.943800
2   Fri 18th Nov 2016  1074.160340
3   Sat 19th Nov 2016  2980.857860
4   Sun 20th Nov 2016  1919.723960
5   Mon 21st Nov 2016   884.279340
6   Tue 22nd Nov 2016   869.071070
7   Wed 23rd Nov 2016   760.289260
8   Thu 24th Nov 2016  2481.689270
9   Fri 25th Nov 2016  2745.990070
10  Sat 26th Nov 2016  2273.413250
11  Sun 27th Nov 2016  2630.414900
12  Mon 28th Nov 2016   817.322310
13  Tue 29th Nov 2016  1766.876030
14  Wed 30th Nov 2016   469.388420

I would like to change the format of the date column to this format YYYY-MM-DD. The dataframe consists of more than 200 rows, and every day new rows will be added, so I need to find a way to do this automatically.

This link is not helping because it sets the dates like this dates = ['30th November 2009', '31st March 2010', '30th September 2010'] and I can't do it for every row. Anyone knows a way to solve this?

Upvotes: 0

Views: 540

Answers (1)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17064

Dateutil will do this job.

from dateutil import parser

print df
df2 = df.copy()
df2.date = df2.date.apply(lambda x: parser.parse(x))
df2

Output:

enter image description here

Upvotes: 2

Related Questions