How to get unique values from multiple columns in a pandas groupby
One more alternative is to use GroupBy.agg
with set
df.groupby('c').agg(set)
l1 l2
c
1 {a, b} {d, b}
2 {c, b} {e, f}
You can do it with apply
:
import numpy as np
g = df.groupby('c')['l1','l2'].apply(lambda x: list(np.unique(x)))
Alternatively, you can use agg
:
g = df.groupby('c')['l1','l2'].agg(['unique'])