[英]how to sort a group by with aggregate in pandas
I have a dataset of books, i am grouping it and using aggregations我有一个书籍数据集,我正在对其进行分组并使用聚合
df_books.groupby('Author').agg({'Reviews':['min','max'], 'User Rating':'sum' })
giving me this output给我这个 output
Reviews min max User Rating sum
Author
Abraham Verghese 4866 4866 9.2
Adam Gasiewski 3113 3113 4.4
Adam Mansbach 9568 9568 4.8
Adir Levy 8170 8170 4.8
Admiral William H. McRaven 10199 10199 4.7
... ... ... ...
Walter Isaacson 3014 7827 13.7
William Davis 7497 7497 8.8
William P. Young 19720 19720 9.2
Wizards RPG Team 16990 16990 14.4
Zhi Gang Sha 37 220 9.2
I need order by 'User Rating' sum我需要按“用户评分”总和排序
I assume your original table looks something like this:我假设您的原始表格如下所示:
Author Reviews UserRating
0 Abraham_Verghese 4866 9.2
1 Adam_Gasiewski 3113 4.4
2 Adam_Mansbach 9568 4.8
3 Adir_Levy 8170 4.8
4 Admiral_William_H.McRaven 10199 4.7
5 Walter_Isaacson 3014 13.7
6 William_Davis 7497 8.8
7 William_P.Young 19720 9.2
8 Wizards_RPG_Team 16990 14.4
9 Zhi_Gang_Sha 37 9.2
Then I will do the groupby()
, followed by droplevel()
and reset_index()
to level out the column headers, then sort_values()
by the column you want然后我将执行
groupby()
,然后是droplevel()
和reset_index()
以平整列标题,然后按您想要的列sort_values()
df = df.groupby('Author')\
.agg({'Reviews':['min','max'], 'UserRating':'sum' })\
.droplevel(1,1)\
.reset_index()\
.sort_values(by='UserRating', ascending=False)
df.columns = ['Author', 'ReviewsMin', 'ReviewsMax', 'UserRatingSum']
print(df)
Output: Output:
Author ReviewsMin ReviewsMax UserRatingSum
8 Wizards_RPG_Team 16990 16990 14.4
5 Walter_Isaacson 3014 3014 13.7
0 Abraham_Verghese 4866 4866 9.2
7 William_P.Young 19720 19720 9.2
9 Zhi_Gang_Sha 37 37 9.2
6 William_Davis 7497 7497 8.8
2 Adam_Mansbach 9568 9568 4.8
3 Adir_Levy 8170 8170 4.8
4 Admiral_William_H.McRaven 10199 10199 4.7
1 Adam_Gasiewski 3113 3113 4.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.