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

Pandas show both unique counts and unique values?

Let’s say I have a dataframe like this:

import pandas as pd

df = pd.DataFrame([
    ('a', 'aa'),
    ('b', 'aa'),
    ('c', 'bb'),
    ('d', 'bb'),
    ('e', 'cc'),
    ('f', 'cc'),
    ('h', 'cc')
], columns=['group', 'id'])

I do a groupby to show the count of unique values and also the unique values themself. Here is what I am doing now:

df1 = df.groupby(["id"])["group"].nunique()
print(df1)
id
aa    2
bb    2
cc    3
df2 =  df.groupby(['id'])['group'].agg(['unique'])
print(df2)
id           
aa     [a, b]
bb     [c, d]
cc  [e, f, h]

However, I am trying to have these two shown next to each other (one column shows the count and one shows the values as shown below. Is there any way to get that?

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

id    count values
aa    2     [a, b]
bb    2     [c, d]
cc    3     [e, f, h]

>Solution :

res = df.groupby('id')['group'].agg(count='nunique', values='unique')

Output

>>> res

    count     values
id                  
aa      2     [a, b]
bb      2     [c, d]
cc      3  [e, f, h]
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