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

我在 groupby 上應用了 sum(),我想對最后一列的值

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

問題描述

給定以下數據幀

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

然后您可以像以前一樣使用 groupbysum,此外您可以按兩列 [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(),我想對最后一列的值進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久热综合 | 亚洲资源网 | 三级黄色片网站 | 午夜伦理福利 | av黄页| 国产午夜一区 | av不卡一区| 死神来了4无删减版在线观看 | 黄色一级免费视频 | 少妇一级淫片免费观看 | 黄网站在线播放 | 欧美人与野 | 性免费视频 | 在线一区二区三区四区 | www.夜夜操 | 亚洲综合在线视频 | 国产综合视频 | 色婷婷免费视频 | 成人午夜在线观看 | 国产精品偷乱一区二区三区 | 黄色福利 | 国产一区二区三区在线 | 超碰av在线播放 | 国产精品乱码一区二区三区 | 欧美成人一级片 | 久久中文字幕视频 | 一级黄色录像片 | 毛片导航 | 午夜在线国语中文字幕视频 | 亚洲国产精 | 中文字幕免费在线观看 | 秋霞国产| 91亚洲精品在线 | 国产精品一区二区三区免费 | 日韩无遮挡| 欧美一区二区三区在线播放 | 亚洲第一色网 | 国产免费黄色片 | av综合网站| 欧美一区二区三区在线播放 | 国v精品久久久网 |