pythonpandasrolling-computation

Rolling Z-score applied to pandas dataframe


I would like to compute a rolling Z-score for one of my columns in my dataframe:

import pandas as pd

values = [1,2,3,4,5]

d1= {'vol': values}

df= pd.DataFrame(d1)

Is there a way of doing this similar to this:

df['mean'] = df.rolling(2).mean()

Maybe with:

from scipy import stats
stats.zscore(df)

EDIT: Found this approach in a similar post:

def zscore_func(x):
    return (x[-1] - x[:-1].mean())/x[:-1].std(ddof=0)
df.rolling(window=3).apply(zscore_func)

Solution

  • window = 2
    target_column = 'vol'
    roll = df[target_column].rolling(window)
    df['z-score'] = (df[target_column] - roll.mean()) / roll.std()