【发布时间】:2017-10-11 13:20:42
【问题描述】:
我有一个数据框,其中包含房屋 (ID) 的每小时 kWh 能源消耗 (Consumption),持续几个月,例如:
ID Consumption
DateTime
2016-07-01 01:00:00 1642 0.703400
2016-07-01 02:00:00 1642 0.724033
2016-07-01 03:00:00 1642 0.747300
2016-07-01 04:00:00 1642 0.830450
2016-07-01 05:00:00 1642 0.704917
2016-07-01 06:00:00 1642 0.708467
2016-07-01 07:00:00 1642 0.806533
2016-07-01 08:00:00 1642 0.774483
2016-07-01 09:00:00 1642 0.724833
2016-07-01 10:00:00 1642 0.721900
2016-07-01 11:00:00 1642 0.729450
2016-07-01 12:00:00 1642 0.757233
2016-07-01 13:00:00 1642 0.744667
这里 DateTime 是 type 的索引。我的目标是找出一周内每个小时的平均消耗量和方差,即(24*7 = 168 小时)
HourOfWeek Consumption
1 0.703400
2 0.724033
...
168 0.876923
我试过了
print (df.groupby(df.index.week)['Consumption'].transform('mean'))
但这并没有给出正确的结果,如何在 pandas 中做到这一点?任何帮助将不胜感激。
【问题讨论】:
-
df.groupby(df.index.dayofweek * df.index.hour)['Consumption'].transform('mean')?
标签: pandas time-series