-2

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?

Rajat
  • 111
  • 13
  • `df.rolling('3M').mean()` ? – ansev Oct 20 '20 at 21:51
  • What do you want to do with nans? – zabop Oct 20 '20 at 21:52
  • _but that doesn't seem to work._ Can you elaborate? Please provide a [mcve], as well as the current and expected output. – AMC Oct 21 '20 at 00:22
  • Does this answer your question? [Replace NaN or missing values with rolling mean or other interpolation](https://stackoverflow.com/questions/25234782/replace-nan-or-missing-values-with-rolling-mean-or-other-interpolation) – Mr. T Oct 21 '20 at 08:19

1 Answers1

0

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
zabop
  • 6,750
  • 3
  • 39
  • 84