Then you can use, groupby and sum as before, in addition you can sort values by two columns [user_ID, amount] and ascending=[True,False] refers ascending order of user and for each user descending order of amount:new_df = df.groupby(['user_ID','product_id'], sort=True).sum().reset_index()new_df = new_df.sort_values(by = ['user_ID', 'amount'], ascending=[True,False])print(new_df)输出: user_ID product_id amount1 1 456 60 1 87 32 1 788 33 2 456 64 2 788 3 这篇关于我在groupby上应用了sum(),我想对最后一列的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 09:52