How to get unique values from multiple columns in a pandas groupby

后端 未结 2 1082
無奈伤痛
無奈伤痛 2021-02-01 18:51

Starting from this dataframe df:

df = pd.DataFrame({\'c\':[1,1,1,2,2,2],\'l1\':[\'a\',\'a\',\'b\',\'c\',\'c\',\'b\'],\'l2\':[\'b\',\'d\',\'d\',\'f\',\'e\',\'f\']         


        
2条回答
  •  遥遥无期
    2021-02-01 19:26

    Alternatively, you can use agg:

    g = df.groupby('c')['l1','l2'].agg(['unique'])
    

提交回复
热议问题