Skip to content
Advertisement

Pandas Multiindex to CSV without duplicate Index

For the life of me I can’t figure out how to remove the duplicate index items created when writing a multiindex dataframe to CSV. While there is this answer out there, it doesn’t apply to me per se because my second level has all different values.

This is a chunk of the dataframe I have, it just goes on for each month

JavaScript

But in CSV it looks something like

JavaScript

I hope for it to look exactly just like the dataframe so that means in CSV it would look like

JavaScript

I have tried df.index.to_series().duplicates() but it doesn’t work because each of my index pairs are not duplicates. I’ve also tried using index.tolist() then looping through to change with no luck.

What’s the right approach here?

Thanks in advance!

Advertisement

Answer

It seems pandas does not provide directly such functionality, perhaps to ensure that the generated csv files can be read back, as mentioned in the comments above. I faced with the same problem when generating a table for report in rst format. Finally, I decided to process the generated csv by an external script. The script replaces the repeating values in columns with spaces, thus achieving desired result. Note: this script assumes commas as csv separators.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement