简体   繁体   English

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

[英]Extract year/month from date python to new columns

I have a column with dates in object type 我有一个列在对象类型中的日期

> df['created_at_first']

Thats the result 多数民众赞成的结果

created_at_first
2018-07-01 02:08:06
2018-06-05 01:39:30
2018-05-16 21:18:48

I would like to create new columns for year, month, day, hour. 我想创建年,月,日,小时的新列。 So I get something like that: 所以我得到了类似的东西:

year  month  day  hour 
2018   07    01   02
2018   06    05   01
2018   05    16   21

How can I manage it? 我该如何管理它?

Maybe: 也许:

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

One flexible approach is to use operator.attrgetter with pd.concat . 一种灵活的方法是将operator.attrgetterpd.concat一起使用。 Such an approach enables you to specify an arbitrary list of properties, which are then extracted via the pd.Series.dt accessor. 这种方法使您可以指定任意属性列表,然后通过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

Setup 设定

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 will be helpful to get required result 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

official Document: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html 官方文件: https//pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html

output: 输出:

            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

You can try using strftime and then to split on '-' as given inside strftime('%Y-%m-%d-%H') function. 你可以尝试使用strftime ,然后在strftime('%Y-%m-%d-%H')函数内给出'-'分割。 The code: 编码:

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

If you want all the columns in a single dataframe use pd.concat() along axis=1 . 如果希望单个数据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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM