两个数据框之间的相关性 [英] Correlation between two dataframes
本文介绍了两个数据框之间的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人问过类似的问题,但我还没有一个清晰的答案。请原谅我再次询问。我有两个数据框,我只想第一个数据框与第二个数据列的相关性。以下代码正是我想要的:
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 df2中的col:
打印df1 ['Y']。corr(df2 [col])
但似乎我不应该遍历数据框。我希望像
df1.corr(df2)
<这样简单的事情/ pre>
应该完成工作。
解决方案您可以使用
corrwith
:>> df2.corrwith(df1.Y)
X1 0.051002
X2 -0.339775
X3 0.076935
dtype:float64
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?
解决方案You can use
corrwith
:>>> df2.corrwith(df1.Y) X1 0.051002 X2 -0.339775 X3 0.076935 dtype: float64
这篇关于两个数据框之间的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文