假设我有一个具有多索引的数据帧,构造如下:
import numpy as np
import pandas as pd
ids = ['a', 'b', 'c']
hours = np.arange(24)
data = np.random.random((len(ids),len(hours)))
df = pd.concat([pd.DataFrame(index = [[id]*len(hours), hours], data = {'value':data[ind]}) for ind, id in enumerate(ids)])
df.index.names = ['ID', 'hour']
它看起来像这样:
value
ID hour
a 0 0.020479
1 0.059987
2 0.053100
3 0.406198
4 0.452231
...
c 19 0.150493
20 0.617098
21 0.377062
22 0.196807
23 0.954401
我想做的是 for each 站点获得一个新的24小时时间序列,但使用5小时滚动平均计算.
我知道我可以做df.rolling(5, center = True, on = 'hour')
个小时,但问题是它没有考虑到小时是周期性的-即,0小时的滚动平均值应该是22、23、0、1和2小时的平均值.
做这件事的好方法是什么?
谢谢!