Wrong decimal calculations with pandas
Question: I have a data frame (df) in pandas with four columns and I want a new column
to represent the mean of this four columns: df['mean']= df.mean(1)

    
    
      1    2    3    4   mean 
    NaN  NaN  NaN  NaN      NaN  
    5.9  5.4  2.4  3.2    4.225  
    0.6  0.7  0.7  0.7    0.675  
    2.5  1.6  1.5  1.2    1.700  
    0.4  0.4  0.4  0.4    0.400 
    

So far so good. But when I save the results to a csv file this is what I
found:

    
    
    5.9,5.4,2.4,3.2,4.2250000000000005
    0.6,0.7,0.7,0.7,0.6749999999999999
    2.5,1.6,1.5,1.2,1.7
    0.4,0.4,0.4,0.4,0.4
    

I guess I can force the format in the mean column, but any idea why this is
happenning?

I am using winpython with python 3.3.2 and pandas 0.11.0


Answer: You could use the `float_format` parameter:

    
    
    import pandas as pd
    import io
    
    content = '''\
    1    2    3    4   mean 
    NaN  NaN  NaN  NaN      NaN  
    5.9  5.4  2.4  3.2    4.225  
    0.6  0.7  0.7  0.7    0.675  
    2.5  1.6  1.5  1.2    1.700  
    0.4  0.4  0.4  0.4    0.400'''
    
    df = pd.read_table(io.BytesIO(content), sep='\s+')
    df.to_csv('/tmp/test.csv', float_format='%g', index=False)
    

yields

    
    
    1,2,3,4,mean
    ,,,,
    5.9,5.4,2.4,3.2,4.225
    0.6,0.7,0.7,0.7,0.675
    2.5,1.6,1.5,1.2,1.7
    0.4,0.4,0.4,0.4,0.4
    

