当我尝试按列值对DataFrame排序并使用白色()函数打印时,它显示重复的行,而不是所需的结果
regions = country_features['world_region']
happines = []
counts = []
reg = []
for region in regions:
hap = country_features.loc[country_features['world_region'] == region, 'happiness_score'].mean()
count = len(country_features[country_features['world_region'] == region])
happines.append(hap)
counts.append(count)
reg.append(region)
region_happines = pd.DataFrame({'region':reg,
'happiness_score' : happines,
'country_count':counts})
region_happines
region_happines.happiness_score = pd.to_numeric(region_happines.happiness_score)
sorted = region_happines.sort_values(by='happiness_score', ascending=False)
sorted.head(5)
我想按列值对DataFrame进行排序,并且希望它能正确排序
1条答案
按热度按时间1tu0hz3e1#
应简化解决方案的第一部分:
因为
happiness_score
列是每组的平均值,未转换为数值。