問(wèn)題描述
給定以下數(shù)據(jù)幀
第一列是客戶的 ID,第二列是他購(gòu)買的產(chǎn)品的 ID,如果是當(dāng)天購(gòu)買的產(chǎn)品數(shù)量,則表示金額"(日期也考慮在內(nèi)).客戶每天可以隨心所欲地購(gòu)買許多產(chǎn)品.我想計(jì)算客戶購(gòu)買每種產(chǎn)品的總次數(shù),所以我應(yīng)用了 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
現(xiàn)在我想對(duì)每組中的金額總和進(jìn)行排序.有什么幫助嗎?
now I want to sort the sum of amount in each group. Any help?
推薦答案
假設(shè) df
是:
然后您可以像以前一樣使用 groupby
和 sum
,此外您可以按兩列 [user_ID, amount]
和ascending=[True,False]
表示用戶升序,每個(gè)用戶的金額降序:
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:
輸出:
這篇關(guān)于我在 groupby 上應(yīng)用了 sum(),我想對(duì)最后一列的值進(jìn)行排序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!