1

I have a data frame in pandas with float64 data types.

0.0 0.0 0.0 -0.25 -0.25881904510000003 0.93301270189 0.01
0.0 0.0 0.0 -0.25347200456 -0.20221757232999998 0.9459703993 0.01
0.0 0.0 0.0 -0.25608634008000003 -0.14493185931 0.95572723231 0.01
.
.
.

I would like the data columns to be aligned and to have scientific formatting, like this

0.00E+00 0.00E+00 0.00E+00 -2.50E-01 -2.59E-01 9.33E-01 1.00E-02
0.00E+00 0.00E+00 0.00E+00 -2.53E-01 -2.02E-01 9.46E-01 1.00E-02
0.00E+00 0.00E+00 0.00E+00 -2.56E-01 -1.45E-01 9.56E-01 1.00E-02
.
.
.

Which I can have displayed by setting

pd.set_option('display.float_format', '{:.2E}'.format)

But how do I export the data to have the same formatting?

To export the data I am using

df_rayfile.to_csv(output_file, header=None, index=None, sep=' ')

Setting the display output has no effect on the exported data and by using float_format option in to_cvs I was also not able to get the same output as with setting the display format.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
miquo
  • 75
  • 1
  • 6
  • 2
    Have you tried `float_format='%.2E'`? – IanS Sep 12 '16 at 15:47
  • 1
    @IanS you should state to pass that as a param for `to_csv` – EdChum Sep 12 '16 at 15:49
  • @IanS it works, thanks. I tried to pass float_format='{:.2E}'.format to to_csv. – miquo Sep 12 '16 at 19:51
  • @IanS where can I find some documentation on types of formatting available with float_format parameter ? Pandas API reference on to_csv does not give a lot of information here, it is basically a list of parameters with short description. – miquo Sep 12 '16 at 20:06
  • @miquo agreed the documentation is a bit sparse, but any old-style format string (as described [here](http://www.python-course.eu/python3_formatted_output.php) for instance) should work. – IanS Sep 13 '16 at 08:10

0 Answers0