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

后端 未结 2 1081
無奈伤痛
無奈伤痛 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:08

    You can do it with apply:

    import numpy as np
    g = df.groupby('c')['l1','l2'].apply(lambda x: list(np.unique(x)))
    
    0 讨论(0)
  • 2021-02-01 19:26

    Alternatively, you can use agg:

    g = df.groupby('c')['l1','l2'].agg(['unique'])
    
    0 讨论(0)
提交回复
热议问题