Groupby and count the number of unique values (Pandas)

Viewed 3985

I have a dataframe with 2 variables: ID and outcome. I'm trying to groupby ID first, and count the number of unique values of outcome within that ID.

df
ID    outcome
1      yes
1      yes
1      yes
2      no
2      yes
2      no

Expected output:

ID    yes    no
1      3     0
2      1     2

My code df[['PID', 'outcome']].groupby('PID')['outcome'].nunique() gives the number of the unique value itself, such that:

ID
1   2
2   2

But I need the counts of the yes and no, how can I achieve that? Thanks!

5 Answers
Related