获取相关的降序键列表
Get descending key list of correlation
我尝试获取与列 '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名的名字吗?
temp = df[df.columns[1:]].corr()['price']
temp.sort_values(by='NameOfTheColumn', ascending=False)
temp.index.values.tolist()[:5]
我认为这应该可行。
这是我在@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
我尝试获取与列 'price'
.
这是我的代码:
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名的名字吗?
temp = df[df.columns[1:]].corr()['price']
temp.sort_values(by='NameOfTheColumn', ascending=False)
temp.index.values.tolist()[:5]
我认为这应该可行。
这是我在@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