Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I have these lines of code reading and writing an excel:
df = pd.read_excel(file_path, sheet_name, header=[0, 1])
df.to_excel(output_path, index=False)
When it tries to write the excel I get the following error:
NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented
I have no idea why this is happening, and I cannot find a concrete answer online.
Please help.
That is because you are having multi index in your dataframe.
You can either reset_index() or drop your level=1 index.
If you don't want index then you can insert a column as index
df.insert(0,df.index,inplace=False) #something like this
Multi-index columns can actually be exported to Excel. You just have to set index=True.
So for the example the solution becomes...
df = pd.read_excel(file_path, sheet_name, header=[0, 1])
df.to_excel(output_path, index=True)
NB. This is true as of Pandas version 1.2.0
Multi-index columns cannot be exported to Excel. It is possible to transform the multi-index to single index, and then export it to excelc.
df = df.reset_index()
df.to_excel('file_name.xlsx', index=False)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.