[英]How do i slice pandas Series with DatetimeIndex and put it in a DataFrame by rows?
我有使用DatetimeIndex的pandas Series對象:
2016-01-04 00:00:00 11.6
2016-01-04 01:00:00 11.7
2016-01-04 02:00:00 17.1
2016-01-04 03:00:00 36.5
2016-01-04 04:00:00 19.0
2016-01-04 05:00:00 7.5
2016-01-04 06:00:00 18.7
2016-01-04 07:00:00 14.5
2016-01-04 08:00:00 6.5
2016-01-04 09:00:00 41.9
2016-01-04 10:00:00 37.3
2016-01-04 11:00:00 30.3
2016-01-04 12:00:00 38.4
2016-01-04 13:00:00 26.0
2016-01-04 14:00:00 31.0
2016-01-04 15:00:00 32.3
2016-01-04 16:00:00 29.4
2016-01-04 17:00:00 43.5
2016-01-04 18:00:00 26.9
2016-01-04 19:00:00 20.4
2016-01-04 20:00:00 16.5
2016-01-04 21:00:00 10.9
2016-01-04 22:00:00 12.8
2016-01-04 23:00:00 16.4
2016-01-05 00:00:00 10.1
2016-01-05 01:00:00 9.4
2016-01-05 02:00:00 12.0
2016-01-05 03:00:00 8.6
2016-01-05 04:00:00 16.9
2016-01-05 05:00:00 8.4
...
我如何按日期切片這個系列,並把它放在一個日期/小時pandas Dataframes像這樣:
00 01 02 03 04 ... 22 23
2016-01-04 11.6 1.7 17.1 36.5 19.0 ... 12.8 16.4
2016-01-05 10.1 9.4 12.0 8.6 16.9 ... 12.8 16.4
...
PS:對不起我的英文
首先使用to_frame
將系列轉換為數據to_frame
,然后使用s.index.strftime('%H')
和index.date
提取小時和日期。 最后在這些列上轉動以轉換為“寬”格式:
df = s.to_frame()
df['hour'] = df.index.strftime('%H')
df['date'] = df.index.date
df2 = df.pivot(columns='hour', index='date')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.