问题
I'm pretty new to python/pandas and I have a dataframe that looks something like this:
id name color
id_1 alex blue
id_2 james yellow
id_1 sara black
id_4 dave pink
id_4 lin grey
id_2 aly red
I want to group by id and get the values in the other two columns as a list:
id name color
id_1 [alex,sara] [blue,black]
id_2 [james,aly] [yellow,red]
id_4 [dave,lin] [pink,grey]
Is there an easy way to do that?
回答1:
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]
来源:https://stackoverflow.com/questions/45436938/pandas-group-two-columns-based-on-value-in-another-column