pandas 两个数据帧之间的相关性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33157528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:02:45  来源:igfitidea点击:

Correlation between two dataframes

pythonpandasdataframe

提问by TPM

Similar questions have been asked, but I've not seen a lucid answer. Forgive me for asking again. I have two dataframes, and I simply want the correlation of the first data frame with each column in the second. Here is code which does exactly what I want:

有人问过类似的问题,但我还没有看到清晰的答案。原谅我又问了。我有两个数据框,我只想将第一个数据框与第二个中的每一列相关联。这是完全符合我想要的代码:

df1=pd.DataFrame( {'Y':np.random.randn(10) } )
df2=pd.DataFrame( {'X1':np.random.randn(10), 'X2':np.random.randn(10) ,'X3':np.random.randn(10) } )
for col in df2:
   print df1['Y'].corr(df2[col])

but it doesn't seem like I should be looping through the dataframe. I was hoping that something as simple as

但似乎我不应该遍历数据帧。我希望像这样简单的事情

df1.corr(df2) 

ought to get the job done. Is there a clear way to perform this function without looping?

应该完成工作。有没有一种明确的方法可以在不循环的情况下执行此功能?

回答by Alexander

You can use corrwith:

您可以使用corrwith

>>> df2.corrwith(df1.Y)
X1    0.051002
X2   -0.339775
X3    0.076935
dtype: float64