2

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.

2
  • can you update the sample data as well so others can understand the format differences Commented Oct 4, 2019 at 2:19
  • The url of sample2.xlsx has been added in the question. Commented Oct 4, 2019 at 3:05

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Divya. How to convert a pandas dataframe to openpyxl object to save it?
sorry, but why convert to openpyxl object to save when we can directly save pandas dataframe?
It is because saving with pandas cannot keep the multi-indexed columns structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.