计算所有列熊猫的滚动平均值

时间:2020-10-20 21:48:41

标签: python pandas dataframe rolling-average

我有以下数据框:

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

我想用3个月的滚动平均值代替NaN值。我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果您将NaN设为0,则可以执行以下操作:

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

这将为您提供:

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