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

Group by and count of a pandas dataframe column

I have a pandas dataframe where I am trying to group a column and get the unique values of another column.

  id    name   
  a-1   sfdad
  a-1   sfdad
  a-1   oiuoi
  a-2   oqrwq
  a-2   oqrwq
  a-2   ljlsg
  a-2   uoire

I do the group by using:

  df = df.groupby('id')['name'].agg(['unique'])
  df = df.reset_index()

and then when i do the count of column "unique" using the below statement, it does not align with the results of df[‘unique’]. Length of df[‘unique’] and the below statement seems to be different.

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

 df.groupby('id')['name'].nunique()

Result

id   unique                count
a-1  [sfdad,oiuoi]         2
a-2  [oqrwq,ljlsg,uoire]   3

>Solution :

You can compute several things at once with agg. This will necessarily be aligned:

df.groupby('id')['name'].agg(['unique', 'nunique'])

output:

                    unique  nunique
id                                 
a-1         [sfdad, oiuoi]        2
a-2  [oqrwq, ljlsg, uoire]        3
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