如何计算python pandas数据框中行之间的相关性

Nao*_*man 5 python performance linear-regression correlation pandas

我有大数据框,我需要有效地计算数据框行和给定值列表之间的相关性。例如:

dfa= DataFrame(np.zeros((1,4)) ,columns=['a','b','c','d'])
dfa.ix[0] = [2,6,8,12]
a   b   c   d
2.0 6.0 8.0 12.0
dfb= DataFrame([[2,6,8,12],[1,3,4,6],[-1,-3,-4,-6]], columns=['a','b','c','d'])
    a   b   c   d
0   2   6   8   12
1   1   3   4   6
2  -1  -3  -4  -6
Run Code Online (Sandbox Code Playgroud)

我希望得到:

0    1
1    0.5
2   -0.5
Run Code Online (Sandbox Code Playgroud)

我尝试了很多版本,例如:

dfb.T.corrwith(dfa.T, axis=0)
Run Code Online (Sandbox Code Playgroud)

但我得到的是很多南的

mak*_*kis 6

首先,请注意最后 2 个相关性是 1 和 -1,而不是您预期的 0.5 和 -0.5。

解决方案

dfb.corrwith(dfa.iloc[0], axis=1)
Run Code Online (Sandbox Code Playgroud)

结果

0    1.0
1    1.0
2   -1.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)