Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check duplicated indices for each subset of values in pandas dataframe

I have the following dataframe:

import pandas as pd

df_test = pd.DataFrame(data=[['AP1', 'House1'],
                             ['AP1', 'House1'], 
                             ['AP2', 'House1'], 
                             ['AP3', 'House2'], 
                             ['AP4','House2'], 
                             ['AP5', 'House2']],
                       columns=['AP', 'House'],
                       index=[0, 1, 2, 0, 1, 1])

I need to check at each subset of values of a column and see if there are duplicated indices. For example, in column House, we have three entries of House1 and no duplicated indices. But for entry House2 we have one duplicated index 1.

I have tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(f'{df_test.index.duplicated().sum()} repeated entries')

But this gives 3 duplicated entries, since it does not consider each value of the column separately.

>Solution :

A possible solution:

print(df_test.reset_index().duplicated(['index', 'AP']).sum())
print(df_test.reset_index().duplicated(['index', 'House']).sum())

Output:

0
1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading