1

I want to write a multi-index dataframe to excel:

col = [['info', '', 'key'], ['alert', 'date', 'price'], ['alert', 'date', 'amount']]
df = pd.DataFrame(columns = pd.MultiIndex.from_tuples(col))
df.loc[0, :] = np.random.random(3)
df.to_excel('data.xlsx', index = False)

However, an error occurs:

NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.

I checked pandas version : pd.__version__ and the result is '0.25.3'.

How to solve the problem?

Thank you.

enter image description here

4
  • How important is for you to export directly as xlsx? If not very important, you can export as CSV first and then load into Excel. Commented Dec 10, 2019 at 3:16
  • Can you show me the code? Commented Dec 10, 2019 at 3:17
  • Checkout this answer: stackoverflow.com/questions/17349574/… Commented Dec 10, 2019 at 3:19
  • It has been said that this problem has been solved in version 0.17.0 in stackoverflow.com/questions/33633858/…. But why do I have this problem? Commented Dec 10, 2019 at 3:31

3 Answers 3

2

After searching the web, I used pywin32 to solve the problem.

import win32com.client as win32
df.to_excel('data.xlsx', index = True)
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.DisplayAlerts = False
wb = excel.Workbooks.Open('data.xlsx')
excel.Visible = True
ws = wb.Worksheets('Sheet1')
ws.Columns(1).EntireColumn.Delete()
wb.SaveAs('data.xlsx')
excel.Application.Quit()
Sign up to request clarification or add additional context in comments.

Comments

1

To remove the extra column, try resetting the index to 'info' column.

df.set_index(['info'])

Comments

0

Try

df.to_excel(r'data.xlsx', index = True)

ref: https://github.com/pandas-dev/pandas/issues/11292#issuecomment-447150410

4 Comments

if setting index to True, there will be an extra column in the excel file. How to remove that column?
Please refer to the revised question.
Can anyone help?
Adding comment to come back later. Also, I am also waiting on a answer to this question.

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.