更改pandas数据框中的日期格式

时间:2016-12-14 15:59:07

标签: python date datetime

我有这个数据框:

             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

我想将date列的格式更改为此格式 YYYY-MM-DD 。数据框由200多行组成,每天都会添加新行,因此我需要找到一种自动执行此操作的方法。

This link没有帮助,因为它设置的日期类似dates = ['30th November 2009', '31st March 2010', '30th September 2010'],我不能为每一行都这样做。有人知道解决这个问题的方法吗?

1 个答案:

答案 0 :(得分:2)

Dateutil将完成这项工作。

from dateutil import parser

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

输出:

enter image description here