简体   繁体   English

Pandas Groupby 结果基于 value_counts 和升序值

[英]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.通过在堆叠的数据帧上运行sort_values ,您可以强制它按值对整个数据帧进行排序,而这些值首先已在类别内排序。 So don't run highest_medals_countries.sort_values(ascending = False)[:10] and you're fine.所以不要运行highest_medals_countries.sort_values(ascending = False)[:10]就没事了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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