pythonpandasdataframecsvdecimal-point

pandas to_csv arguments float_format and decimal not working for index column


I have the following snippet to export my data as a csv file:

rpm = [0.25, 0.3, 0.5, 0.75, 1.0, 1.5, 2.0]

df = pd.DataFrame(get_some_data_from_somwhere(), index=rpm)

with open('foo.csv', 'w') as f:
    data.to_csv(f, index=True, header=True, decimal=',', sep=' ', float_format='%.3f')

It creates a csv-file that has the following format:

rpm cooling_inner heating_inner cooling_outlet heating_outlet
0.25 303,317 323,372 302,384 324,332

However, I expected having three decimal digits and a comma as decimal sign on my index column, like shown here:

rpm cooling_inner heating_inner cooling_outlet heating_outlet
0,250 303,317 323,372 302,384 324,332

So it seems that the index and decimal sign options are not applied to the index column when exporting dataframes to csv-files using the .to_csv command.

How could I achieve this behaviour since the index option is set True and all values (with exception to the index column) have the right format and decimal sign?

Do I have to handle the index column somehow separately?


Solution

  • I would rewrite your two bottom lines:

    with open('foo.csv', 'w') as f:
        data.to_csv(f, index=True, header=True, decimal=',', sep=' ', float_format='%.3f')
    

    Into

    data.reset_index().to_csv('foo.csv', index=False, header=True, decimal=',', sep=' ', float_format='%.3f')
    

    This is a bit of a workaround, but as you have noticed, the keyword arguments decimal= and float_format= only work on data columns, not on the index.

    What I do instead is to put the index into the dataframe with reset_index and then I tell to_csv(index=False not to save the index to the file (since it is now in the data).

    Also, opening a file stream yourself (with open('foo.csv', 'w') as f:) is better left to pandas, which does this by itself when you just give it a string 'foo.csv' as first argument.