gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:46:26 25 4
gpt4 key购买 nike

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

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

我希望得到:
0    1
1 0.5
2 -0.5

我尝试了很多版本,例如:
dfb.T.corrwith(dfa.T, axis=0)

但我得到的是很多南的

最佳答案

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

解决方案

dfb.corrwith(dfa.iloc[0], axis=1)

结果
0    1.0
1 1.0
2 -1.0
dtype: float64

关于python - 如何计算python pandas数据框中行之间的相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47074894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com