如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:37:48  来源:igfitidea点击:

How to extract the year, month and day in Pandas?

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.monthand dt.day:

你需要dt.yeardt.month并且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

回答by piRSquared

Elegant way to parse string into all three columns with one step

一步将字符串解析为所有三列的优雅方法

df = pd.DataFrame({'fecha_dato':['2016-05-28','2016-06-28','2016-07-28']})

regex = '(?P<Year>[^-]+)-(?P<Month>[^-]+)-(?P<Day>[^-]+)'
pd.concat([df, df.fecha_dato.str.extract(regex).astype(int)], axis=1)

enter image description here

在此处输入图片说明