我有一个数据框,其中包含房屋(ID)每小时的千瓦时能耗(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是类型的索引。我的目标是找到一周中每个小时的平均消耗和方差,即(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')
?df.groupby(df.index.dayofweek * df.index.hour)['Consumption'].transform('mean')
是什么意思? - Zero