[英]Pandas Groupby results coming up based on the value_counts and ascending values
highest_medals_countries = olympics_merged.groupby(['Sport'])['Team'].value_counts()
highest_medals_countries.sort_values(ascending = False)[:10]
輸出:運動隊
Athletics United States 3202
Great Britain 2240
Gymnastics United States 1939
Swimming United States 1622
Gymnastics France 1576
Athletics France 1494
Gymnastics Italy 1345
Swimming Great Britain 1291
Athletics Germany 1254
Gymnastics Hungary 1242
在上面的輸出中,我將根據運動獲得最多獎牌的團隊堆疊在一起,但是當我查看輸出時,運動會根據價值計數出現。 我怎樣才能擺脫這種情況並將國家放在一起進行田徑、體操、游泳等運動?
預期輸出為:
Sport Team
Athletics United States 3202
Great Britain 2240
France 1494
Gymnastics United States 1939
France 1576
Italy 1345
Hungary 1242
Swimming United States 1622
Great Britain 1291
Athletics Germany 1254
通過在堆疊的數據幀上運行sort_values
,您可以強制它按值對整個數據幀進行排序,而這些值首先已在類別內排序。 所以不要運行highest_medals_countries.sort_values(ascending = False)[:10]
就沒事了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.