Chan
Chan

Reputation: 4301

How to write multiIndex-columns excel with pandas

I want to export a multiIndex-column.

I read an excel file (https://drive.google.com/open?id=1G6nE5wiNRf5sip22dQ8dfhuKgxzm4f8E) and exported it with the following code:

df = pd.read_excel('sample.xlsx')
df.to_excel('sample2.xlsx', index = False)

However, sample2.xlsx has different format as sample.xlsx.

For example, there are merged cells in sample.xlsx but not in sample2.xlsx and the blank cells in sample.xlsx become Unnamed:xx.

You can view sample2.xlsx here.

How to solve this problem?

Thank you.

Upvotes: 2

Views: 531

Answers (1)

Divya Dass
Divya Dass

Reputation: 166

Since you working with xlsx files, openpyxl package will do the job.

import openpyxl
wb_obj = openpyxl.load_workbook('sample.xlsx')
wb_obj.save('sample2.xlsx')

Further reading on openpyxl

Upvotes: 1

Related Questions