我在 groupby 上应用了 sum() ,我想对最后一列的值进行排序
I applied sum() on a groupby and I want to sort the values of the last column
给定以下 DataFrame
user_ID product_id amount
1 456 1
1 87 1
1 788 3
1 456 5
1 87 2
... ... ...
第一列是客户的ID,第二列是他购买的产品ID,'amount'表示当天购买的产品数量(日期也带上了)考虑)。客户每天可以购买任意数量的产品。
我想计算客户购买每种产品的总次数,所以我应用了 groupby
df.groupby(['user_id','product_id'], sort=True).sum()
现在我想对每个组中的金额总和进行排序。
有帮助吗?
假设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]
指的是用户的升序和对于每个用户的金额降序:
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
您也可以使用 aggregate()
:
# Make up some example data
df = data.frame (user_ID = as.factor(rep(1:5, each = 5)),
product_id = as.factor(sample(seq(1:10),size = 25, replace = TRUE)),
amount = sample(1:5, size = 25, replace = TRUE))
# Use aggregate with function sum to calculate the amount of products bought by product and customer
aggregate(amount ~ product_id * user_ID , data = df, FUN = sum)
输出:
product_id user_ID amount
1 2 1 3
2 4 1 2
3 6 1 1
4 9 1 5
5 1 2 5
6 3 2 9
7 8 2 1
8 10 2 5
9 2 3 5
10 3 3 5
11 4 3 5
12 5 3 3
13 8 3 5
14 3 4 3
15 4 4 9
16 5 4 2
17 10 4 1
18 2 5 1
19 4 5 4
20 5 5 2
21 10 5 2
这会给你前 5 个最大的:
# n = number of rows you want to return
df.groupby(['user_id'])['amount'].sum().nlargest(n)
给定以下 DataFrame
user_ID product_id amount
1 456 1
1 87 1
1 788 3
1 456 5
1 87 2
... ... ...
第一列是客户的ID,第二列是他购买的产品ID,'amount'表示当天购买的产品数量(日期也带上了)考虑)。客户每天可以购买任意数量的产品。
我想计算客户购买每种产品的总次数,所以我应用了 groupby
df.groupby(['user_id','product_id'], sort=True).sum()
现在我想对每个组中的金额总和进行排序。 有帮助吗?
假设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]
指的是用户的升序和对于每个用户的金额降序:
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
您也可以使用 aggregate()
:
# Make up some example data
df = data.frame (user_ID = as.factor(rep(1:5, each = 5)),
product_id = as.factor(sample(seq(1:10),size = 25, replace = TRUE)),
amount = sample(1:5, size = 25, replace = TRUE))
# Use aggregate with function sum to calculate the amount of products bought by product and customer
aggregate(amount ~ product_id * user_ID , data = df, FUN = sum)
输出:
product_id user_ID amount
1 2 1 3
2 4 1 2
3 6 1 1
4 9 1 5
5 1 2 5
6 3 2 9
7 8 2 1
8 10 2 5
9 2 3 5
10 3 3 5
11 4 3 5
12 5 3 3
13 8 3 5
14 3 4 3
15 4 4 9
16 5 4 2
17 10 4 1
18 2 5 1
19 4 5 4
20 5 5 2
21 10 5 2
这会给你前 5 个最大的:
# n = number of rows you want to return
df.groupby(['user_id'])['amount'].sum().nlargest(n)