krish venkat
krish venkat

Reputation: 1

How to get a rolling_mean for 42 days of S&P 500 using pandas?

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'

Upvotes: 0

Views: 188

Answers (2)

kyramichel
kyramichel

Reputation: 491

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

Upvotes: 0

Murali
Murali

Reputation: 184

You can use rolling() followed by 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

Please change your question title to highlight 'rolling mean in pandas'

Upvotes: 1

Related Questions