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

在 pandas 數(shù)據(jù)框中查找重復(fù)行

find duplicate rows in a pandas dataframe(在 pandas 數(shù)據(jù)框中查找重復(fù)行)
本文介紹了在 pandas 數(shù)據(jù)框中查找重復(fù)行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試在 pandas 數(shù)據(jù)框中查找重復(fù)行.

I am trying to find duplicates rows in a pandas dataframe.

df=pd.DataFrame(data=[[1,2],[3,4],[1,2],[1,4],[1,2]],columns=['col1','col2'])

df
Out[15]: 
   col1  col2
0     1     2
1     3     4
2     1     2
3     1     4
4     1     2

duplicate_bool = df.duplicated(subset=['col1','col2'], keep='first')
duplicate = df.loc[duplicate_bool == True]

duplicate
Out[16]: 
   col1  col2
2     1     2
4     1     2

有沒有辦法添加引用第一個副本(保留的那個)的索引的列

Is there a way to add a column referring to the index of the first duplicate (the one kept)

duplicate
Out[16]: 
   col1  col2  index_original
2     1     2               0
4     1     2               0

注意:在我的情況下,df 可能非常大....

Note: df could be very very big in my case....

推薦答案

使用groupby,新建一列索引,然后調(diào)用duplicated:

Use groupby, create a new column of indexes, and then call duplicated:

df['index_original'] = df.groupby(['col1', 'col2']).col1.transform('idxmin')    
df[df.duplicated(subset=['col1','col2'], keep='first')]

   col1  col2  index_original
2     1     2               0
4     1     2               0

<小時>

詳情

groupby前兩列然后調(diào)用transform + idxmin得到每個組的第一個索引.

I groupby first two columns and then call transform + idxmin to get the first index of each group.

df.groupby(['col1', 'col2']).col1.transform('idxmin') 

0    0
1    1
2    0
3    3
4    0
Name: col1, dtype: int64

duplicated 給了我想要保留的值的布爾掩碼:

duplicated gives me a boolean mask of values I want to keep:

df.duplicated(subset=['col1','col2'], keep='first')

0    False
1    False
2     True
3    False
4     True
dtype: bool

剩下的只是布爾索引.

這篇關(guān)于在 pandas 數(shù)據(jù)框中查找重復(fù)行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 男人视频网站 | 午夜爱爱毛片xxxx视频免费看 | 午夜播放器在线观看 | 国产精品久久久久久福利一牛影视 | 男人天堂99 | 一区二区电影 | 亚洲国产精品美女 | 国产美女久久久 | 成人夜晚看av | 欧美国产在线一区 | av在线一区二区三区 | 日本黄色片免费在线观看 | 久久久青草婷婷精品综合日韩 | 国产91色在线 | 亚洲 | 久久精品国产一区老色匹 | 亚洲狠狠丁香婷婷综合久久久 | 国产成人在线播放 | 91人人看 | 亚洲久久一区 | 国产第1页| 国产午夜精品一区二区三区 | 欧美精品黄 | 成人午夜视频在线观看 | 91精品国产手机 | 久久综合久久久 | 日韩在线免费视频 | 日韩精品一区二区三区中文在线 | 1000部精品久久久久久久久 | 成人3d动漫一区二区三区91 | 久久99精品久久久久久国产越南 | 久久精品视频99 | 国产成视频在线观看 | 在线观看视频91 | 久久国产精品一区二区三区 | 精品福利av导航 | 亚洲精品不卡 | 亚洲国产精品成人无久久精品 | 国产精品永久久久久 | 中国黄色毛片视频 | 自拍偷拍亚洲视频 | 日韩h|