Skip to content
Advertisement

Calculate rolling average for all columns pandas

I have the below dataframe:

df = pd.DataFrame({'a': [2.85,3.11,3.3,3.275,np.NaN,4.21], 'b': [3.65,3.825,3.475,np.NaN,4.10,2.73],
                   'c': [4.3,3.08,np.NaN,2.40, 3.33, 2.48]}, index=pd.date_range('2019-01-01', periods=6, 
                    freq='M'))

This gives the dataframe as below:
                a      b     c
2019-01-31  2.850  3.650  4.30
2019-02-28  3.110  3.825  3.08
2019-03-31  3.300  3.475   NaN
2019-04-30  3.275    NaN  2.40
2019-05-31    NaN  4.100  3.33
2019-06-30  4.210  2.730  2.48

Expected:
                a      b     c
2019-01-31  2.850  3.650  4.30
2019-02-28  3.110  3.825  3.08
2019-03-31  3.300  3.475  **3.69**
2019-04-30  3.275  **3.650**  2.40
2019-05-31  **3.220**  4.100  3.33
2019-06-30  4.210  2.730  2.48

I want to replace the NaN values with the 3 month rolling average. How should I got about this?

Advertisement

Answer

If you take NaNs as 0 into your means, can do:

df.fillna(0,inplace=True)
df.rolling(3).mean()

This will give you:

a   b   c
2019-01-31  NaN NaN NaN
2019-02-28  NaN NaN NaN
2019-03-31  3.086667    3.650000    2.460000
2019-04-30  3.228333    2.433333    1.826667
2019-05-31  2.191667    2.525000    1.910000
2019-06-30  2.495000    2.276667    2.736667

Advertisement