0

I have a pandas dataframe which I have wrangled a bit with multi-indexing and pivoting.

If I ask Ipython to print it to the screen (just by making a selection and hitting enter), it has lovely formatting, like so:

enter image description here

If I then write this to CSV, with a basic command:

pivoted.to_csv(filepath)

I get some rather ugly formatting, like so:

enter image description here

How might I write the CSV file with the same formatting as in the Ipython console (the format is also the same in a regular console, just without the fancy boxes)?

By that I mean the neat hierarchy of column headers and grouping of the 'cru_year' entries.

jramm
  • 6,415
  • 4
  • 34
  • 73
  • you need to write it to an excel file; csv is simply comma separated values; if you want to keep the cells merged use `to_excel` method; – behzad.nouri Mar 04 '14 at 15:15
  • CSV inherently does not support "formatting" You may have better luck with to_excel see here http://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet and scroll down to the 4th answer.. – dartdog Mar 04 '14 at 15:17

0 Answers0