Reputation: 95
If i have a dataframe and i want to merge ID column based on the Name column without deleting any row. How would i do this?
Ex-
Name | ID |
---|---|
John | ABC |
John | XYZ |
Lucy | MNO |
I want to convert the above dataframe into the below one
Name | ID |
---|---|
John | ABC, XYZ |
John | ABC, XYZ |
Lucy | MNO |
Upvotes: 0
Views: 39
Reputation: 862641
Use GroupBy.transform
with join
:
df['ID'] = df.groupby('Name')['ID'].transform(', '.join)
print (df)
Name ID
0 John ABC, XYZ
1 John ABC, XYZ
2 Lucy MNO
Upvotes: 1