获取相关的降序键列表

问题描述 投票:0回答:2

我尝试通过'price'列获得前5个最高相关分数的名称。

这是我的代码:

ls = list(df.corrwith(df['price']))
ls.sort(reverse=True)
ls[0:5]

输出:

[0.9999999999999999,
 0.31555576200285607,
 0.29866047751549785,
 0.2731839437705133,
 0.2673960209310168]

如果我运行此代码:

df[df.columns[1:]].corr()['price']

我将得到这样的输出:

host_since                                     -0.047803
host_response_rate                              0.077262
host_is_superhost                              -0.020062
host_total_listings_count                       0.116733
host_has_profile_pic                           -0.002491
host_identity_verified                         -0.041795
...

有没有办法获得前5名的名称?

python list dataframe correlation
2个回答
1
投票

这是我在@SahilDesai的评论帮助下的解决方案:

df[df.columns[1:]].corr()['price'].sort_values(ascending=False)[:6]

输出:

price           1.000000
accommodates    0.315556
bedrooms        0.298660
cleaning_fee    0.273184
beds            0.267396
bathrooms       0.262596

0
投票
temp = df[df.columns[1:]].corr()['price']

temp.sort_values(by='NameOfTheColumn', ascending=False)

temp.index.values.tolist()[:5]

我认为这应该可行。

© www.soinside.com 2019 - 2024. All rights reserved.