Python 获取相关的降序键列表
我试图通过“价格”列获得前5名最高相关分数的名称 这是我的代码:Python 获取相关的降序键列表,python,list,dataframe,correlation,Python,List,Dataframe,Correlation,我试图通过“价格”列获得前5名最高相关分数的名称 这是我的代码: ls = list(df.corrwith(df['price'])) ls.sort(reverse=True) ls[0:5] 输出: [0.9999999999999999, 0.31555576200285607, 0.29866047751549785, 0.2731839437705133, 0.2673960209310168] price 1.000000 accommodates
ls = list(df.corrwith(df['price']))
ls.sort(reverse=True)
ls[0:5]
输出:
[0.9999999999999999,
0.31555576200285607,
0.29866047751549785,
0.2731839437705133,
0.2673960209310168]
price 1.000000
accommodates 0.315556
bedrooms 0.298660
cleaning_fee 0.273184
beds 0.267396
bathrooms 0.262596
如果我运行此代码:
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
...
有没有办法知道前五名的名字
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]
输出:
[0.9999999999999999,
0.31555576200285607,
0.29866047751549785,
0.2731839437705133,
0.2673960209310168]
price 1.000000
accommodates 0.315556
bedrooms 0.298660
cleaning_fee 0.273184
beds 0.267396
bathrooms 0.262596
df.sort_valuesby='col1',ascending=False您尝试过对值进行排序吗?@SahilDesai它需要进行一些修改才能正常工作。谢谢