Rolling Z-score applied to pandas dataframe

半世苍凉 提交于 2021-01-04 09:20:12

问题


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)

回答1:


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



回答2:


Here is one solution by for loop

n=2
[np.nan]*n+[stats.zscore(df.iloc[x:x+n,0]) for x in range(0,len(df)-n)]
[nan, nan, array([-1.,  1.]), array([-1.,  1.]), array([-1.,  1.])]


来源:https://stackoverflow.com/questions/59596912/rolling-z-score-applied-to-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!