Skip to content Skip to sidebar Skip to footer

Combine Rows By Id

I have the following types of data. How to combine rows by id, and the type record a separate column (all types 10) (or record types, separated by commas in a single column) id,typ

Solution 1:

I think you can use groupby with apply join, but first convert int to str:

df = df.groupby('id')['type'].apply(lambda x: ','.join(x.astype(str))).reset_index()
print (df)
   id        type
0   1  8,2,7,3,10
1   2       3,8,7

Post a Comment for "Combine Rows By Id"