簡體   English   中英

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

[英]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.attrgetterpd.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM