在数据框架中查找相关性

发布于 2025-02-11 19:02:53 字数 465 浏览 2 评论 0原文

我有一个带有列(例如x_1,x_2,.... x_n为列名称)的pandas dataframe(DF)。我想在ITH列和其余列之间找到相关性(Pearson)。

我可以做到这一点的一种方法是使用.corr()函数

correlation = df.corr(method='pearson')
corr_i = correlation['x_i']

,但是此方法位于昂贵,因为它找到了所有列之间的相关性(我只需要一个列)。我能做的另一种方法是,

corr_i =[df['x_i'].corr(df[j], method ='pearson') for j in df.columns.tolist() if j!='x_i']

但我确实认为,考虑到数据框的灵活性,这不是找到相关性的有效方法。谁能用比两个以上的高效方法帮助我?提前致谢。

I have a pandas dataframe(df) that has columns (say x_1,x_2,....x_n as column names). I want to find a correlation (Pearson) between the ith column and the rest of the columns.

One way I can do this is by using the .corr() function

correlation = df.corr(method='pearson')
corr_i = correlation['x_i']

but this method is bit expensive since it finds correlations between all of the columns (all I need is only one column). The other method that I could do is

corr_i =[df['x_i'].corr(df[j], method ='pearson') for j in df.columns.tolist() if j!='x_i']

but I do feel that this is not efficient way of finding correlation given the flexibility of dataframe. Can anyone help me with very efficient method than above two? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-02-18 19:02:53

corrwith()可能是想要的。

假设您有一个带有列C1,C2,C3,C4的数据框架。

那么您应该能够:

df[['c2','c3','c4']].corrwith(df['c1'])

corrwith() might be what are looking for.

Say you had a data frame with columns c1,c2,c3,c4.

Then you should be able to:

df[['c2','c3','c4']].corrwith(df['c1'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文