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
Month Measure
JAN KWH
Max KW
Day/Hour
Peak End Use
Peak Pct
FEB KWH
Max KW
Day/Hour
Peak End Use
Peak Pct
But in CSV it looks something like
JAN KWH
JAN Max KW
JAN Day/Hour
JAN Peak End Use
JAN Peak Pct
FEB KWH
FEB Max KW
FEB Day/Hour
FEB Peak End Use
FEB Peak Pct
I hope for it to look exactly just like the dataframe so that means in CSV it would look like
Jan,KWH
,Max KW
,Day/Hour
and so on
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!
file.write(str(df))
. – Slusher