简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM