Rolling Mean on pandas on a specific column Rolling Mean on pandas on a specific column python-3.x python-3.x

Rolling Mean on pandas on a specific column


To assign a column, you can create a rolling object based on your Series:

df['new_col'] = data['column'].rolling(5).mean()

The answer posted by ac2001 is not the most performant way of doing this. He is calculating a rolling mean on every column in the dataframe, then he is assigning the "ma" column using the "pop" column. The first method of the following is much more efficient:

%timeit df['ma'] = data['pop'].rolling(5).mean()%timeit df['ma_2'] = data.rolling(5).mean()['pop']1000 loops, best of 3: 497 µs per loop100 loops, best of 3: 2.6 ms per loop

I would not recommend using the second method unless you need to store computed rolling means on all other columns.


Edit: pd.rolling_mean is deprecated in pandas and will be removed in future. Instead: Using pd.rolling you can do:

df['MA'] = df['pop'].rolling(window=5,center=False).mean()

for a dataframe df:

          Date    stock  pop0   2016-01-04  325.316   821   2016-01-11  320.036   832   2016-01-18  299.169   793   2016-01-25  296.579   844   2016-02-01  295.334   825   2016-02-08  309.777   816   2016-02-15  317.397   757   2016-02-22  328.005   808   2016-02-29  315.504   819   2016-03-07  328.802   81

To get:

          Date    stock  pop    MA0   2016-01-04  325.316   82   NaN1   2016-01-11  320.036   83   NaN2   2016-01-18  299.169   79   NaN3   2016-01-25  296.579   84   NaN4   2016-02-01  295.334   82  82.05   2016-02-08  309.777   81  81.86   2016-02-15  317.397   75  80.27   2016-02-22  328.005   80  80.48   2016-02-29  315.504   81  79.89   2016-03-07  328.802   81  79.6

Documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html

Old: Although it is deprecated you can use:

df['MA']=pd.rolling_mean(df['pop'], window=5)

to get:

          Date    stock  pop    MA0   2016-01-04  325.316   82   NaN1   2016-01-11  320.036   83   NaN2   2016-01-18  299.169   79   NaN3   2016-01-25  296.579   84   NaN4   2016-02-01  295.334   82  82.05   2016-02-08  309.777   81  81.86   2016-02-15  317.397   75  80.27   2016-02-22  328.005   80  80.48   2016-02-29  315.504   81  79.89   2016-03-07  328.802   81  79.6

Documentation: http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.rolling_mean.html


This solution worked for me.

data['MA'] = data.rolling(5).mean()['pop']

I think the issue may be that the on='pop' is just changing the column to perform the rolling window from the index.

From the doc string: " For a DataFrame, column on which to calculate the rolling window, rather than the index"