Categories
dataframe duplicates pandas python

find duplicate rows in a pandas dataframe

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

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