我在groupby上应用了sum(),我想对最后一列的值进行排序 [英] I applied sum() on a groupby and I want to sort the values of the last column
问题描述
给出以下数据框
user_ID product_id amount
1 456 1
1 87 1
1 788 3
1 456 5
1 87 2
... ... ...
第一列是客户的ID,第二列是他购买的产品的ID,如果在给定日期(还考虑了日期)购买的产品数量,则为金额".客户每天可以购买任意数量的商品.
我想计算客户购买每种产品的总次数,因此我应用了groupby
The first column is the ID of the customer, the second is the ID of the product he bought and the 'amount' express if the quantity of the product purchased on that given day (the date is also taken into consideration). a customer can buy many products each day as much as he wants to.
I want to calculate the total of times each product is bought by the customer, so I applied a groupby
df.groupby(['user_id','product_id'], sort=True).sum()
现在我要对每组中金额的总和进行排序. 有帮助吗?
now I want to sort the sum of amount in each group. Any help?
推荐答案
假设df
是:
user_ID product_id amount
0 1 456 1
1 1 87 1
2 1 788 3
3 1 456 5
4 1 87 2
5 2 456 1
6 2 788 3
7 2 456 5
然后您可以像以前一样使用groupby
和sum
,此外,您还可以按两列对值进行排序[user_ID, amount]
和ascending=[True,False]
指的是用户的升序以及每个用户的金额降序:>
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 amount
1 1 456 6
0 1 87 3
2 1 788 3
3 2 456 6
4 2 788 3
这篇关于我在groupby上应用了sum(),我想对最后一列的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!