我正在尝试按照本教程计算 SMA:https ://www.datacamp.com/community/tutorials/moving-averages-in-pandas
我想获取所有值的 SMA,但我只得到 5 个。我在框架中有 17 个值,我想获取它们的值。如果我增加滚动窗口,我根本得不到 SMA 的任何值,这是为什么?
感谢您的帮助,因为我是 Pandas 的新手
def example(self):
frame = {'date': ['2017-06-19', '2017-06-16', '2017-06-15', '2017-06-14', '2017-06-13', '2017-06-12', '2017-06-09', '2017-06-08', '2017-06-07', '2017-06-06', '2017-06-05', '2017-06-02', '2017-06-01', '2017-05-31'], 'indexes': ['146.3400', '142.2700', '144.2900', '145.1600', '146.5900', '145.4200', '148.9800', '154.9900', '155.3700', '154.4500', '153.9300', '155.4500', '153.1800', '152.7600']}
df = pd.DataFrame(frame)
df['SMA'] = df.iloc[:, 1].rolling(window=4).mean()
print(df.head())
输出:
date indexes SMA
0 2017-06-19 146.3400 NaN
1 2017-06-17 142.2700 NaN
2 2017-06-16 144.2900 NaN
3 2017-06-15 145.1600 144.5150
4 2017-06-14 146.5900 144.5775
相关分类