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

问题描述

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

~\Anaconda3\lib\site-packages\pandas\__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()