给定一个DataFrame,如何在一个groupby中计算N个周期(5)的特定列的滚动相关系数?
我的尝试,我觉得我很接近,但遗漏了一些简单的事情......
使用Corr
df['cor'] = df.groupby('DIMENSION').val1.apply(lambda x: x.rolling(window=5).corr())
使用另一个堆栈溢出建议
df['cor'] = df.groupby('DIMENSION').val1.apply(lambda x: x.rolling(window=5).corr()[df'val1']df['index'])
尝试使用corrwith默认值
df['cor'] = df.groupby('DIEMNSION').val1.apply(lambda x: x.rolling(window=5).corrwith())
数据帧:
Dimension,Val1,Index
A,15677,0
A,19191,1
A,19961,2
A,10004,3
A,17899,4
A,10933,5
A,12953,6
A,19431,7
A,17637,8
A,13310,9
A,10113,10
A,19108,11
A,17169,12
A,13859,13
A,11466,14
A,13673,15
A,15049,16
B,14996,17
B,17679,18
B,12944,19
C,19927,20
C,10659,21
C,14561,22
C,18842,23
C,13784,24
C,14562,25
C,10145,26
答案 0 :(得分:2)
我们试试这个:
def mytrans(x):
return pd.DataFrame(x['Val1'].rolling(window=5).corr(x['Index']))
df['corr'] = df.groupby('Dimension')[['Val1','Index']].apply(mytrans)
df