1

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

user2082695
  • 250
  • 3
  • 14

2 Answers2

2

The answers seem correct. Floating point numbers cannot be perfectly represented on our systems. There are bound to be some differences. Read The Floating Point Guide.

>>> a = 5.9+5.4+2.4+3.2
>>> a / 4
4.2250000000000005

As you said, you could always format the results if you want to get only a fixed number of points after the decimal.

>>> "{:.3f}".format(a/4)
'4.225'
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
2

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
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • What is '%g', is it the presentation type from [here](http://docs.python.org/2/library/string.html#format-specification-mini-language)? – Andy Hayden Aug 09 '13 at 13:54
  • @AndyHayden: Essentially, yes. But since it is using the percent-sign, I think float_format is using the old-style string format [documented here](http://docs.python.org/2/library/stdtypes.html#string-formatting-operations). – unutbu Aug 09 '13 at 14:55