whitefang1993
whitefang1993

Reputation: 1716

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

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.

Upvotes: 10

Views: 16556

Answers (4)

ayoub mlaouah
ayoub mlaouah

Reputation: 494

You can simply set index=True instead of False

Upvotes: 6

user1680868
user1680868

Reputation: 11

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

Upvotes: 0

Vahan Sargsyan
Vahan Sargsyan

Reputation: 28

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)

Upvotes: -3

Rakesh Shinde
Rakesh Shinde

Reputation: 58

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

Upvotes: 0

Related Questions