如何在 Pandas 中提取年、月和日?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41138190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract the year, month and day in Pandas?
提问by yanachen
I have a dataframe which has a column named 'fecha_dato'. It stores the date like '2016-05-28'. I want to extract the 2016, 05 and 28 as int from fecha_dato as new columns named year, month and day. I use the iterator way but it is too slow. Is there any efficient way to do this ?
我有一个数据框,其中有一列名为“fecha_dato”。它存储像“2016-05-28”这样的日期。我想从 fecha_dato 中提取 2016、05 和 28 作为 int 作为名为年、月和日的新列。我使用迭代器的方式,但它太慢了。有什么有效的方法可以做到这一点吗?
回答by jezrael
You need dt.year
, dt.month
and dt.day
:
df['year'] = df.fecha_dato.dt.year
df['month'] = df.fecha_dato.dt.month
df['day'] = df.fecha_dato.dt.day
Sample:
样本:
df = pd.DataFrame({'fecha_dato':['2016-05-28','2016-06-28','2016-07-28']})
#if dtype is not datetime, cast it
df.fecha_dato = pd.to_datetime(df.fecha_dato)
df['year'] = df.fecha_dato.dt.year
df['month'] = df.fecha_dato.dt.month
df['day'] = df.fecha_dato.dt.day
print (df)
fecha_dato year month day
0 2016-05-28 2016 5 28
1 2016-06-28 2016 6 28
2 2016-07-28 2016 7 28