用熊猫计算滚动平均值

时间:2020-11-11 18:52:08

标签: python pandas

sp500['42d'] = np.round(pd.rolling_mean(sp500['Close'], window=42), 2)

早上好,我正在尝试计算滚动平均值。但是我回来了

AttributeError: module 'pandas' has no attribute 'rolling_mean'

我试图将代码编写为

sp500['42d'] = np.round(pd.DataFrame.rolling(sp500['Close'], window=42), 2).mean()

我被赋予了

TypeError: unsupported operand type(s) for *: 'Rolling' and 'float'

我确实相信我已经运行了代码,但是它排除了我认为很重要的'2',否则为什么会出现在代码中。

有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

我相信您已经找到了答案,但这应该可以解决:

sp500['42d'] = np.round(pd.DataFrame.rolling(sp500['Close'], window=42).mean(),2)

或者:

sp500['42d'] = np.round(sp500['Close'].rolling(window=42).mean(),2)