久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美一区免费 | 超碰精品在线 | 亚洲免费视频在线观看 | 毛片在线看片 | 91精品国产色综合久久不卡蜜臀 | 亚洲 欧美 日韩 精品 | 91av久久久| 91福利网址| 国产色黄| 特级丰满少妇一级aaaa爱毛片 | 国产精品96久久久久久 | 成人不卡| 视频一区二区三区中文字幕 | 日日碰碰 | 国产精品1区 | 久久久久久99 | 自拍偷拍在线视频 | 日韩在线视频一区 | 国产精品久久久久久妇女6080 | 国产丝袜一区二区三区免费视频 | 国产高清自拍视频在线观看 | 久久久成人一区二区免费影院 | 亚洲高清在线观看 | 天天干天天插天天 | 一区二区三区高清 | 一级a爱片性色毛片免费 | 久久人人网 | 一区二区三区精品视频 | 亚洲精品中文字幕在线观看 | 国产成人精品a视频 | 久久综合伊人 | 久久精品视频在线观看 | 国产一区二区三区 | 亚洲 欧美 综合 | 色资源在线 | 成人免费一区二区 | 国产精品视频播放 | 天天干天天色 | 久久久久久亚洲精品 | 国产精品999 | 羞羞视频一区二区 |