Merge multiple rows (having some non string values) with same ID into one delimited row in pandas

最后都变了- 提交于 2019-12-11 03:17:00

问题


I have a dataset like this:

ID    Name
 1       a
 1       b
 1       2
 1       3
 2      er
 2     get
 2  better
 3     123
 3    cold
 3    warm
 3   sweet
 3    heat

and I want to group together this data such that data column "name" having same "id" is merged together using a delimiter. Something like this:

ID                      Name
 1                   a,b,2,3
 2             er,get,better
 3  123,cold,warm,sweet,heat

and so on.

Can anyone provide me a pythonic way of doing this?


回答1:


Use ','.join in a groupby

df.groupby('ID').Name.apply(','.join)

ID
1                     a,b,c,d
2               er,get,better
3    hot,cold,warm,sweet,heat
Name: Name, dtype: object

Reset the index if you need those same two columns

df.groupby('ID').Name.apply(','.join).reset_index()

   ID                      Name
0   1                   a,b,c,d
1   2             er,get,better
2   3  hot,cold,warm,sweet,heat

If for some reason you have non string items

df.assign(Name=df.Name.astype(str)).groupby('ID').Name.apply(','.join).reset_index()

   ID                      Name
0   1                   a,b,c,d
1   2             er,get,better
2   3  hot,cold,warm,sweet,heat


来源:https://stackoverflow.com/questions/52433293/merge-multiple-rows-having-some-non-string-values-with-same-id-into-one-delimi

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