Count unique values using pandas groupby

前端 未结 3 1936

I have data of the following form:

df = pd.DataFrame({
    \'group\': [1, 1, 2, 3, 3, 3, 4],
    \'param\': [\'a\', \'a\', \'b\', np.nan, \'a\', \'a\', np.na         


        
3条回答
  •  再見小時候
    2020-12-01 05:31

    This is just an add-on to the solution in case you want to compute not only unique values but other aggregate functions:

    df.groupby(['group']).agg(['min','max','count','nunique'])
    

    Hope you find it useful

提交回复
热议问题