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

Count Value Exclude Duplicated ID

I have dataframe

df1 = pd.DataFrame({'id': ['1','2','2','3','3','4','5'],
                    'event': ['Basket','Soccer','Soccer','Basket','Soccer','Basket','Soccer']})

I want to count unique values of event but exclude the repeated id. The result I expect are:

event   count   
Basket  3       
Soccer  3       

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

>Solution :

This will work:

df1.groupby('event').agg({'id':lambda x: len(pd.unique(x))})

# OR

df1.groupby(['event']).agg(['nunique'])

Output:

enter image description here

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