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

從數據框中刪除重復項,基于兩列 A,B,在另一

Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C(從數據框中刪除重復項,基于兩列 A,B,在另一列 C 中保持具有最大值的行) - IT屋-程序員軟件開發技術
本文介紹了從數據框中刪除重復項,基于兩列 A,B,在另一列 C 中保持具有最大值的行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 pandas 數據框,其中包含根據兩列(A 和 B)的重復值:

I have a pandas dataframe which contains duplicates values according to two columns (A and B):

A B C
1 2 1
1 2 4
2 7 1
3 4 0
3 4 8

我想刪除在 C 列中保持最大值的行的重復項.這將導致:

I want to remove duplicates keeping the row with max value in column C. This would lead to:

A B C
1 2 4
2 7 1
3 4 8

我不知道該怎么做.我應該使用 drop_duplicates() 嗎?

I cannot figure out how to do that. Should I use drop_duplicates(), something else?

推薦答案

你可以使用 group by:

You can do it using group by:

c_maxes = df.groupby(['A', 'B']).C.transform(max)
df = df.loc[df.C == c_maxes]

c_maxes 是每個組中 C 最大值的Series,但長度和索引相同df.如果您還沒有使用過 .transform,那么打印 c_maxes 可能是一個好主意,看看它是如何工作的.

c_maxes is a Series of the maximum values of C in each group but which is of the same length and with the same index as df. If you haven't used .transform then printing c_maxes might be a good idea to see how it works.

使用 drop_duplicates 的另一種方法是

Another approach using drop_duplicates would be

df.sort('C').drop_duplicates(subset=['A', 'B'], take_last=True)

不確定哪個更有效,但我猜是第一種方法,因為它不涉及排序.

Not sure which is more efficient but I guess the first approach as it doesn't involve sorting.

pandas 0.18 開始,第二個解決方案是

From pandas 0.18 up the second solution would be

df.sort_values('C').drop_duplicates(subset=['A', 'B'], keep='last')

或者,或者,

df.sort_values('C', ascending=False).drop_duplicates(subset=['A', 'B'])

無論如何,groupby 解決方案的性能似乎要好得多:

In any case, the groupby solution seems to be significantly more performing:

%timeit -n 10 df.loc[df.groupby(['A', 'B']).C.max == df.C]
10 loops, best of 3: 25.7 ms per loop

%timeit -n 10 df.sort_values('C').drop_duplicates(subset=['A', 'B'], keep='last')
10 loops, best of 3: 101 ms per loop

這篇關于從數據框中刪除重復項,基于兩列 A,B,在另一列 C 中保持具有最大值的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲视频欧美视频 | 精品欧美黑人一区二区三区 | 欧美一级大片免费观看 | 午夜久久久 | 亚洲男人的天堂网站 | www.午夜| 精精国产xxxx视频在线播放 | 99精品国产一区二区三区 | 自拍 亚洲 欧美 老师 丝袜 | 久久1区 | 看羞羞视频 | 午夜激情小视频 | 国产一区| 色婷婷久久久久swag精品 | 精品欧美激情在线观看 | 亚洲国产精品一区二区www | 日本a∨精品中文字幕在线 亚洲91视频 | 黄色一级大片在线免费看产 | 人人插人人| 天天天天天天操 | 久久久精品一区二区三区 | 日韩高清中文字幕 | 不用播放器看的av | 9久久精品| 欧美中文字幕一区二区三区 | 婷婷久 | 亚洲三级国产 | 久久久久久久久综合 | 日韩高清在线观看 | 一本色道精品久久一区二区三区 | www.97zyz.com| 嫩草一区二区三区 | 日本精品一区二区三区四区 | 久久99国产精品久久99果冻传媒 | 毛色毛片免费看 | 一区二区三区四区免费视频 | 欧美区日韩区 | 国产精品免费观看 | 97caoporn国产免费人人 | 久久久一二三区 | 国产aⅴ爽av久久久久久久 |