如何使用熊猫获得标准普尔 500 指数 42 天的rolling_mean?


sp500['42d']=np.round(pd.rolling_mean(sp500['Close'],window=42),2)
~Anaconda3libsite-packagespandas__init__.py in __getattr__(name)
212 
213   return Panel
--> 214 raise AttributeError("module 'pandas' has no attribute '{}'".format(name))
215 
216 
AttributeError: module 'pandas' has no attribute 'rolling_mean'

您可以先使用rolling(),然后使用mean()

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,100, size=(100, 1)), columns=['close_price'])
df['rolling_mean_42d'] = df['close_price'].rolling(window=42).mean()
df

请更改您的问题标题以突出显示"熊猫中的滚动平均值">

sp500['42d'] = sp500['Close'].rolling(window=42).mean()

最新更新