I have pandas Series object with DatetimeIndex:
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
...
How i can slice this series by dates and put it in a dates/hours pandas Dataframes like this:
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: Sorry my English
First convert your series to a dataframe with to_frame
, then extract the hours and date with s.index.strftime('%H')
and index.date
. Finally pivot on these column to convert to "wide" format:
df = s.to_frame()
df['hour'] = df.index.strftime('%H')
df['date'] = df.index.date
df2 = df.pivot(columns='hour', index='date')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.