使用 groupby
和 agg
通过带有 tolist
的自定义函数:
Use groupby
and agg
by custom function with tolist
:
df = df.groupby('id').agg(lambda x: x.tolist())
print (df)
name color
id
id_1 [alex, sara] [blue, black]
id_2 [james, aly] [yellow, red]
id_4 [dave, lin] [pink, grey]