1
我有一系列值,我正在计算给定表的每一行的皮尔森相关性。熊猫系列与整个数据框之间的相关性
我该怎么做?
例子:
import pandas as pd
v = [-1, 5, 0, 0, 10, 0, -7]
v1 = [1, 0, 0, 0, 0, 0, 0]
v2 = [0, 1, 0, 0, 1, 0, 0]
v3 = [1, 1, 0, 0, 0, 0, 1]
s = pd.Series(v)
df = pd.DataFrame([v1, v2, v3], columns=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
# Here I expect ot do df.corrwith(s) - but won't work
使用Series.corr()
来计算,预计产量
-0.1666666666666666 # correlation with the first row
0.83914639167827343 # correlation with the second row
-0.35355339059327379 # correlation with the third row
谢谢,什么是新手的错误......正是我所需要的 – bluesummers
没问题怎么样,如果数据框有更多的列,你会忽略它吗?这意味着你只想计算只有匹配列进行索引的相关性,而忽略其他索引。 – bluesummers
请检查编辑是否为你想要的。 – jezrael