在熊猫数据框中查找重复的行
问题描述:
我正在尝试在熊猫数据框中找到重复的行.
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
,创建新的索引列,然后调用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
前两列,然后调用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
其余只是布尔索引.