简体   繁体   中英

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]

Output : Sport Team

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

In the above output, I am stacking the teams with the most number of medals based on sport together but when I look at the output the sports are coming up based on the value counts. How can I get rid of this and put countries together for athletics , Gymnastics, Swimming, etc?

Expected output is:

 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

By running sort_values on your stacked dataframe you force it to sort the entire dataframe by value whereas the values were already sorted within the categories in the first place. So don't run highest_medals_countries.sort_values(ascending = False)[:10] and you're fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM