python - 如何在 Pandas V17 中复制 moving.sum()

标签 python pandas rolling-computation

我正在尝试计算以下数据的 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/

相关文章:

python - 序列化错误 "c:\path\path"[Errno 13] 权限被拒绝 : C:\\path\\path

python - pip freeze 显示 appdirs、打包、pyparsing、已安装六个

python Pandas : What causes slowdown in different column selection methods?

r - 如何计算相邻行的平均值?

python - 如何使用 ffmpeg-python 在 filter_() 中使用高级选项?

Python - 理解 CSV 模块和 line_num 对象

python - 如何解决值错误: Columns must be same length as key?

python - 交换 Pandas 字符串列中的两个子字符串

Python Polars 滚动计数

tableau-api - 如何在tableau中聚合表计算