Search code examples
pythonpandasdataframecountunique

Groupby and count the number of unique values (Pandas)


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!


Solution

  • How about pd.crosstab?

    In [1217]: pd.crosstab(df.ID, df.outcome)
    Out[1217]: 
    outcome  no  yes
    ID              
    1         0    3
    2         2    1