0

I'm trying to output a Pandas dataframe into an excel file using pandas dataframe need to merge rows with same value in column pandas.

eg :

pd.DataFrame({' Name': ['ravi', 'ravi', 'manu'],
'Bag': ['123', '123', '129'],
'Serial Number': ['336', '337','335']})

Need to merge both the rows (Name and Bag) as they are having same values

I was not able to merge those rows using pandas

1 Answers1

0

If need merge same values in Excel convert first columns to MultiIndex:

df = pd.DataFrame({' Name': ['ravi', 'ravi', 'manu'],
                    'Bag': ['123', '123', '129'],
                    'Serial Number': ['336', '337','335']})

df = df.set_index([' Name', 'Bag'])
df.to_excel('file.xlsx', merge_cells=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • :1: FutureWarning: In a future version of pandas all arguments of DataFrame.set_index except for the argument 'keys' will be keyword-only. This warning is being raised and the rows are stiil not merged – Lenate John Dec 07 '22 at 08:21
  • @LenateJohn - Problem is with your data in question? Or with real data which are different? – jezrael Dec 07 '22 at 08:55