Pandas: Group two columns based on value in another column

我与影子孤独终老i 提交于 2021-01-27 13:07:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!