pandas correlation matrix between each pair groupby item

為{幸葍}努か 提交于 2019-12-03 16:31:34

With df as above, make a pivot table:

dfp = df.pivot('date','sym')
print(dfp)
           close        
sym            A   B   C
date                    
2014-01-01    10  20  33
2014-01-02    11  22  32
2014-01-03    12  23  31
2014-01-04    13 NaN  30

pandas will calculate the pairwise coefficients:

print(dfp.corr())
              close                    
sym               A         B         C
      sym                              
close A    1.000000  0.981981 -1.000000
      B    0.981981  1.000000 -0.981981
      C   -1.000000 -0.981981  1.000000

But if you want to prettify it, check out seaborn:

import seaborn as sns
sns.corrplot(dfp, annot=True)

result:

After get groups:

sym
A    [nan,1.00,2.00,...]
B    [nan,1.00,2.00,...]
C    [nan,1.00,2.00,...]

I created a DataFrame df2

df2=DataFrame()
df2['A']=groups['A']
df2['B']=groups['B']
df2['C']=groups['C']

df2.corr()

This method can get the correlation via data of groups. However, not perfect. How to convert a groups to a DataFrame like this? Loop keys of groups? I need to continue to try.

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