我有一列包含对象类型的日期
> df['created_at_first']
这就是结果
created_at_first
2018-07-01 02:08:06
2018-06-05 01:39:30
2018-05-16 21:18:48
我想为年、月、日、小时创建新的列。 所以我得到了类似的东西:
year month day hour
2018 07 01 02
2018 06 05 01
2018 05 16 21
我该如何管理它?
最佳答案
也许:
df['created_at_first'] = pd.to_datetime(df['created_at_first'])
df['year'] = df['created_at_first'].dt.year
df['month'] = df['created_at_first'].dt.month
df['day'] = df['created_at_first'].dt.day
df['hour'] = df['created_at_first'].dt.hour
关于python - 从日期 python 中提取年/月到新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51926466/