Home > Net >  pandas ExcelWriter merge but keep value that's already there
pandas ExcelWriter merge but keep value that's already there

Time:12-07

I have a few small data frames that I'm outputting to excel on one sheet. To make then fit better, I need to merge some cells in one table, but to write this in xlsx writer, I need to specify the data parameter. I want to keep the data that is already written in the left cell from using the to_excel() bit of code. Is there a way to do this without having to specify the data parameter? Or do I need to lookup the value in the dataframe to put in there.

For example: df.to_excel(writer, 'sheet') gives similar to the following output:

enter image description here

Then I want to merge across C:D for this table without having to specify what data should be there (because it is already in column C), using something like: worksheet.merge_range('C1:D1', cell_format = fmat) etc. to get below:

enter image description here

Is this possible? Or will I need to lookup the values in the dataframe?

CodePudding user response:

Is this possible? Or will I need to lookup the values in the dataframe?

You will need to lookup the data from the dataframe. There is no way in XlsxWriter to write formatting on top of existing data. The data and formatting need to be written at the same time (apart from Conditional Formatting which can't be used for merging anyway).

  • Related