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

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

find duplicate rows in a pandas dataframe(在 pandas 數(shù)據(jù)框中查找重復(fù)行)
本文介紹了在 pandas 數(shù)據(jù)框中查找重復(fù)行的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(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

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

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

<小時(shí)>

詳情

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

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ū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
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ù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 精品亚洲一区二区三区 | 精品一区二区三区四区在线 | 国产免费让你躁在线视频 | 毛片免费看的 | 亚洲福利网 | 欧美日韩精品一区二区三区四区 | 日韩精品一区二区在线观看 | 欧美精品网站 | 二区成人| 黑人巨大精品欧美一区二区免费 | 亚洲精品一区久久久久久 | www久| 美日韩视频 | 黄视频免费 | 欧美美乳 | 国产成人在线视频 | 欧美区日韩区 | 中文字幕一区二区三区四区五区 | 精品福利在线 | 国产区视频在线观看 | 欧美在线一区二区三区 | 亚洲一区电影 | 成人小视频在线 | 亚洲一区久久 | 日韩免费视频一区二区 | 日韩成人在线观看 | 久久精品中文 | 在线看免费 | 四虎伊人| 三级黄视频在线观看 | 亚洲网站在线播放 | 日韩欧美一区在线 | 中文在线a在线 | 欧美激情在线播放 | 黄色国产区 | 国产精品视频在线播放 | 国产精品美女久久久久久免费 | 亚洲一区免费在线 | 久久久久久国产 | 国产久 | 高清久久久 |