我正在尝试计算以下数据的 3 天滚动总和:
Date Qty
01/01/2019 4.15
02/01/2019 12.39
03/01/2019 14.15
04/01/2019 12.15
05/01/2019 3.26
06/01/2019 6.23
07/01/2019 15.89
08/01/2019 5.55
09/01/2019 12.49
10/01/2019 9.4
11/01/2019 9.11
12/01/2019 9.18
13/01/2019 13.45
14/01/2019 4.52
我尝试过:
data['Rolling_3_day'] = data['Qty'].rolling(3).sum()
但是我收到以下错误:
AttributeError: 'Series' object has no attribute 'rolling'
我认为问题是 Pandas V18 添加了 .rolling,但我有 Pandas V17。但我现在无法更新 pandas。 有没有办法可以使用 V17 计算滚动总和?
最佳答案
对于小窗口,您可以shift
:
df['rolling_3d'] = np.sum([df['Qty'].shift(i) for i in range(3)], axis=0)
关于python - 如何在 Pandas V17 中复制 moving.sum(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66057294/