0

I have a dataframe like this:

enter image description here I want to merge rows with name and also merge category according to name column. I mean if Name column rows has same name then merge category data and rest of the data will be the same :

The output I want like this: enter image description here

the goal is to remove duplicate according by the name and merge category column data into one column

1 Answers1

2

UPDATED Code for multiple columns:

df = pd.DataFrame({'Name':["Call Building & Property Maintenance Ltd", "Call Building & Property Maintenance Ltd"],
'Category':['Fibreglass Roofing', 'Chimney Repairs'], 'Another_column':['Fibreglass Roofing', 'Chimney Repairs']})
df[['Category', 'Another_column']] = df.groupby('Name')[['Category', 'Another_column']].transform(lambda x: ','.join(x))
df.drop_duplicates()

Output:

    Name    Category    Another_column
0   Call Building & Property Maintenance Ltd    Fibreglass Roofing,Chimney Repairs  Fibreglass Roofing,Chimney Repairs
meti
  • 1,921
  • 1
  • 8
  • 15