Search code examples
pythonpandasmeanrolling-computation

how to get a rolling mean with mean from previous window


I am desperatly searching for a solution with pandas. Maybe you could help me.

I am looking for a rolling mean with consideration of the previous mean.

df looks like this:

index count
0 4
1 6
2 10
3 12

now, using the rolling(window=2).mean() function I would get something like this:

index count r_mean
0 4 NaN
1 6 5
2 10 8
3 12 11

I would like to consider the mean from the first calculation, like this:

index count r_mean
0 4 NaN
1 6 5
2 10 7.5
3 12 9.5

where,

row1: (4+6)/2=5

row2: (5+10)/2=7.5

row3: (7.5+12)/2=9.75

thank you in advance!


Solution

  • EDIT: There is actually the method ewm implemented in pandas that can do this calculation

    df['res'] = df['count'].ewm(alpha=0.5, adjust=False, min_periods=2).mean()
    

    Original answer: Here is a way. as everything can be develop with coefficient being power of 2.

    # first create a series with power of 2
    coef = pd.Series(2**np.arange(len(df)), df.index).clip(lower=2)
    
    df['res'] = (coef.div(2)*df['count']).cumsum()/coef
    
    print(df)
       index  count   res
    0      0      4  2.00
    1      1      6  5.00
    2      2     10  7.50
    3      3     12  9.75
    

    You can mask the first value with df.loc[0, 'res'] = np.nan if needed