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