Export to excel for multiindex columns
See original GitHub issueHi All,
I’m just using 0.17 for testing exporting multiindex column dataframe. When i’m pivot table dataframe with X column as index (and without “columns” argument), which will not create multiindex column,
pivot table code (without columns argument):
df.pivot_table(index=["X"], columns=[], aggfunc={"Y": len,"Z":np.sum}, fill_value=0)
df.to_excel(FILE_NAME)
the output using to_excel() follow new format:
But, the question arise when i’m trying to pivot table X column as index (and with “columns” argument), which will create multiindex columns. Then, i’m trying to export it using to_excel() and still follow old format.
pivot table code (with columns argument):
df.pivot_table(index=["X"], columns=["A"], aggfunc={"Y": len,"Z":np.sum}, fill_value=0)
df.to_excel(FILE_NAME)
I don’t know if this newest excel export format only applied to single column index, not multicolumn index. Was this case expected or there is something wrong?
Thanks
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
How to export pandas dataframe with Multi-index columns to ...
One trick is write first level of MultiIndex separately and then skip first row for write another values - DataFrame without first level:...
Read more >pandas.DataFrame.to_excel — pandas 0.18.1 documentation
pandas.DataFrame.to_excel¶ ... Column label for index column(s) if desired. If None is given, and header and index are True, then the index names...
Read more >Pandas to_excel: Writing DataFrames to Excel Files - Datagy
The easiest way to save a Pandas DataFrame to an Excel file is by passing a path to the .to_excel() method. This will...
Read more >How to export pandas dataframe with Multi-index columns to ...
One trick is write first level of MultiIndex separately and then skip first row for write another values - DataFrame without first level:...
Read more >How to flatten MultiIndex Columns and Rows in Pandas
1. MultiIndex columns: use get_level_values() · 2. Flatten columns: use to_flat_index() · 3. Flatten columns: join column labels · 4. Flatten rows: flatten...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@chris-b1 : Correct, the concern is more to the feature consistency. I assume if new format will be applied to both single column and multi index column. Thanks for clarifying this!!
@jreback : Yeah, it’s perfectly works for doing both write and read from excel. But, it seems more elegant if the index column going up one level, see the following example:
But then, i found new error (i don’t know if this already fixed or not). When i’m trying to write excel with not including index (index = False), it throw exception.
The code is like this
Any idea?
Had the same error issue and writing here as a reference to tell what fix it in my case:
Having the index=True fixed my case, the export from df to excel showed the multi Indexing in perfect understood position.
filename = os.path.join(outputDir, "named_df.xls") # .xls or .xlsx doesn't matter
df.to_excel(filename, ### index=True)