从日期python到新列提取年/月

Mic*_*el 3 python datetime dataframe pandas

我有一个列在对象类型中的日期

> 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
Run Code Online (Sandbox Code Playgroud)

我想创建年,月,日,小时的新列.所以我得到了类似的东西:

year  month  day  hour 
2018   07    01   02
2018   06    05   01
2018   05    16   21
Run Code Online (Sandbox Code Playgroud)

我该如何管理它?

U10*_*ard 5

也许:

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
Run Code Online (Sandbox Code Playgroud)