久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我在 groupby 上應(yīng)用了 sum(),我想對(duì)最后一列的值

I applied sum() on a groupby and I want to sort the values of the last column(我在 groupby 上應(yīng)用了 sum(),我想對(duì)最后一列的值進(jìn)行排序)
本文介紹了我在 groupby 上應(yīng)用了 sum(),我想對(duì)最后一列的值進(jìn)行排序的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

給定以下數(shù)據(jù)幀

user_ID  product_id  amount
   1       456          1
   1        87          1
   1       788          3
   1       456          5
   1        87          2
  ...      ...         ...

第一列是客戶的 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

df.groupby(['user_id','product_id'], sort=True).sum()

現(xiàn)在我想對(duì)每組中的金額總和進(jìn)行排序.有什么幫助嗎?

now I want to sort the sum of amount in each group. Any help?

推薦答案

假設(shè) 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

然后您可以像以前一樣使用 groupbysum,此外您可以按兩列 [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:

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

這篇關(guān)于我在 groupby 上應(yīng)用了 sum(),我想對(duì)最后一列的值進(jìn)行排序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚欧精品| 青青草原综合久久大伊人精品 | 91精品国产综合久久小仙女图片 | 天天干天天插 | 亚洲精品中文字幕在线观看 | 久久免费观看一级毛片 | 亚洲色欲色欲www | 国产小视频在线 | 午夜精品久久久久久久久久久久 | 免费簧片视频 | 人人看人人射 | 久久国产区 | 最新av在线播放 | 国产视频一区二区 | 男人午夜视频 | 日本不卡在线视频 | 色www精品视频在线观看 | 日本视频在线 | 亚洲永久入口 | 亚洲视频在线一区 | 欧美高清视频在线观看 | 欧美毛片免费观看 | 久久综合一区二区 | 日韩一区二区三区视频在线观看 | 国产精品69av| av 一区二区三区 | 中文字幕一二三区 | 黄色一级免费看 | 9久9久9久女女女九九九一九 | 国产日日操 | 久久久久久久国产精品影院 | h肉视频 | 狠狠色综合久久丁香婷婷 | 一级片免费在线观看 | 黄色在线观看网址 | 久久久久一区二区三区四区 | 91国内产香蕉 | 国产精品91视频 | 在线视频国产一区 | 2018天天干天天操 | 亚洲自拍偷拍视频 |