2 回答
TA贡献1752条经验 获得超4个赞
您可以对值进行排序并使用df.rolling(5)['open'].mean()
df = df.sort_values('date')
df['5-day avg'] = df.rolling(5)['open'].mean()
df = df.sort_values('date', ascending=False)
df
Out[184]:
open high low close volume date 5-day avg
0 339.0500 339.6100 336.6200 337.2300 68054244 2020-08-19 337.67000
1 338.3400 339.1000 336.6100 338.6400 38733908 2020-08-18 297.58500
2 337.9400 338.3400 336.8517 337.9100 34496002 2020-08-17 257.26700
3 336.4100 337.4200 335.6200 336.8400 47260390 2020-08-14 216.87900
4 336.6100 338.2514 335.8300 336.8300 41816146 2020-08-13 176.79074
5229 138.6250 139.1093 136.7812 137.8750 7431500 1999-11-05 136.76874
5230 136.7500 137.3593 135.7656 136.5312 7907500 1999-11-04 NaN
5231 136.0000 136.3750 135.1250 135.5000 7222300 1999-11-03 NaN
5232 135.9687 137.2500 134.5937 134.5937 6516900 1999-11-02 NaN
5233 136.5000 137.0000 135.5625 135.5625 4006500 1999-11-01 NaN
TA贡献1796条经验 获得超4个赞
在具有窗口大小的熊猫中尝试滚动方法来计算滚动平均值并移动结果。
df['5-Day Avg'] = df['open'].rolling(5).mean().shift(periods=-4)
添加回答
举报