[英]Extract year/month from date python to new columns
我有一个列在对象类型中的日期
> 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
一种灵活的方法是将operator.attrgetter
与pd.concat
一起使用。 这种方法使您可以指定任意属性列表,然后通过pd.Series.dt
访问器提取。
fields = ['year', 'month', 'day', 'hour']
res = pd.concat(attrgetter(*fields)(df['dates'].dt), axis=1, keys=fields)
print(res)
year month day hour
0 2018 7 1 2
1 2018 6 5 1
2 2018 5 16 21
设定
import pandas as pd
from operator import attrgetter
df = pd.DataFrame({'dates': ['2018-07-01 02:08:06',
'2018-06-05 01:39:30',
'2018-05-16 21:18:48']})
df['dates'] = pd.to_datetime(df['dates'])
DatetimeIndex
将有助于获得所需的结果
created_at_first=["2018-07-01 02:08:06","2018-06-05 01:39:30","2018-05-16 21:18:48"]
import pandas as pd
df=pd.DataFrame({'ColumnName':created_at_first})
df['year'] = pd.DatetimeIndex(df['ColumnName']).year
df['month'] = pd.DatetimeIndex(df['ColumnName']).month
df['day'] = pd.DatetimeIndex(df['ColumnName']).day
df['hour'] = pd.DatetimeIndex(df['ColumnName']).hour
官方文件: https : //pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html
输出:
columnName year month day hour
0 2018-07-01 02:08:06 2018 7 1 2
1 2018-06-05 01:39:30 2018 6 5 1
2 2018-05-16 21:18:48 2018 5 16 21
你可以尝试使用strftime
,然后在strftime('%Y-%m-%d-%H')
函数内给出'-'
分割。 编码:
created_at_first=["2018-07-01 02:08:06","2018-06-05 01:39:30","2018-05-16 21:18:48"]
df=pd.DataFrame({'ColumnName':created_at_first})
df['ColumnName']= pd.to_datetime(df['ColumnName'])
df2 = pd.DataFrame(df.ColumnName.dt.strftime('%Y-%m-%d-%H').str.split('-').tolist(),
columns=['Year','Month','Day','Hour'],dtype=int)
df2
Year Month Day Hour
0 2018 07 01 02
1 2018 06 05 01
2 2018 05 16 21
如果希望单个数据pd.concat()
所有列都沿着axis=1
使用pd.concat()
。
pd.concat((df,df2),axis=1)
ColumnName Year Month Day Hour
0 2018-07-01 02:08:06 2018 07 01 02
1 2018-06-05 01:39:30 2018 06 05 01
2 2018-05-16 21:18:48 2018 05 16 21
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.