Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe SMA Calculation

Is there any simple tool/lib that can help me easily calculate the Simple Moving Average SMA(N) of dataframe ?

                             GLD       SMA(5)
Date                                
2005-01-03 00:00:00+00:00  43.020000  Nan
2005-01-04 00:00:00+00:00  42.740002  Nan
2005-01-05 00:00:00+00:00  42.669998  Nan
2005-01-06 00:00:00+00:00  42.150002  Nan
2005-01-07 00:00:00+00:00  41.840000  ..
2005-01-10 00:00:00+00:00  41.950001  ..
2005-01-11 00:00:00+00:00  42.209999  ..
2005-01-12 00:00:00+00:00  42.599998  ..
2005-01-13 00:00:00+00:00  42.599998  ..
2005-01-14 00:00:00+00:00  42.320000  ..
like image 591
Ken Ho Avatar asked Aug 25 '16 06:08

Ken Ho


People also ask

How do you calculate moving average in a data frame?

In Python, we can calculate the moving average using . rolling() method. This method provides rolling windows over the data, and we can use the mean function over these windows to calculate moving averages. The size of the window is passed as a parameter in the function .

How do you calculate exponential moving average in pandas?

We can use the pandas. DataFrame. ewm() function to calculate the exponentially weighted moving average for a certain number of previous periods.


1 Answers

df['SMA(5)'] = df.GLD.rolling(5).mean()
df

enter image description here

like image 80
piRSquared Avatar answered Sep 18 '22 10:09

piRSquared