Search code examples
pandasmeandifferencerolling-computation

Calculate the rolling (mean difference)^2 pandas


Hi I'm trying to create a 'mean difference squared' column in a dataframe using the rolling 3 day mean.

results wise I would like to get a 3rd column with the following values from top down (NaN, NaN, 26, 8, 8, 8) with 26 for example calculated as (1-5)^2 + (6-5)^2 + (8-5)^2, the mean squared value 8 starts (6-8)^2....and so on.

I've written the first two lines of code, not sure how to write further lines to achieve the desired results. Any help appreciated.

df = pd.DataFrame({'Data':[1, 6, 8, 10, 12, 14]})
df['mean'] = df.rolling(window=3).mean()
df['mean difference_squared'] = ........

Solution

  • In your case

    df.Data.rolling(window=3).apply(lambda x : sum((x-x.mean())**2),raw=True)
    Out[173]: 
    0     NaN
    1     NaN
    2    26.0
    3     8.0
    4     8.0
    5     8.0
    Name: Data, dtype: float64