Search code examples
pythoncsvpandasdataframedecimal-point

pandas: float_format and decimal sign not working properly


I am trying to write a pandas DataFrame df to a csv-file using pandas' to_csv method with the following line:

df.to_csv(f, index=False, header=False, decimal=',', sep=' ', float_format='%.3f')

Which gives a csv-file like the following:

295.998 292.500 293.000 293.000
295.998 292.500 293.000 293.000
295.998 292.500 293.000 293.000

So the float_format option works pretty well, as all numbers have three decimal digits. However, the decimal option (decimal = ',') does not seem to work, since the decimal sign is a dot and not a comma.

What I am interested is something like the following:

295,998 292,500 293,000 293,000
295,998 292,500 293,000 293,000
295,998 292,500 293,000 293,000

How can I convience pandas to use three decimal digits and the desired comma as decimal sign?


Solution

  • This behaviour is probably a bug in pandas version 0.15.2 since it works fine after updating to version 0.16.2 using the following command:

    sudo pip3 install -U pandas
    

    Which could be checked by

    import pandas as pd
    pd.__version__
    

    Giving

    '0.16.2'
    

    By using the commands provided in the question the resulting csv-file looks as desired:

    295,998 292,500 293,000 293,000
    295,998 292,500 293,000 293,000
    295,998 292,500 293,000 293,000