Aasheet Kumar
Aasheet Kumar

Reputation: 351

Issue with rounding

I am really new to python and having difficulty with this issue. I have to take the following information from a data frame and provide its mean [ the answer is suppose to return a single number]

Here is the column from the data frame:

   Country
China                  93.0
United States         286.0
Japan                 149.0
United Kingdom        124.0
Russian Federation    214.0
Canada                296.0
Germany               165.0
India                  26.0
France                166.0
South Korea           221.0
Italy                 109.0
Spain                 106.0
Iran                  119.0
Australia             231.0
Brazil                 59.0
Name: Energy Supply per Capita, dtype: float64

Now using the following:

Per_capita = Top15.loc[:,['Energy Supply per Capita']].mean()
Per_capita.iloc[0]

I get:

157.59999999999999

so I tried using the round function:

Per_capita = Per_capita.round(decimals = 1)

I still get the same output if I have any decimal places. It only changes when I put decimals to 0 and it gives 158 which I am assuming is not the answer they are looking for.

if I do not use the iloc function I get a nice 1 decimal answer though

Energy Supply per Capita    157.6

i want to get a 2 decimal answer for the above

Upvotes: 1

Views: 638

Answers (3)

EMiYiTu
EMiYiTu

Reputation: 51

this maybe related with the data 157.59999999999999

take example as follow:

b=157.59999999999999

print np.round(b,decimals=0)

print np.round(b,decimals=1)

print np.round(b,decimals=4)

158.0

157.6

157.6

if change the value of b:

b=157.5932321231341231

print np.round(b,decimals=0)

print np.round(b,decimals=1)

print np.round(b,decimals=2)

158.0

157.6

157.59

this time is working, so this maybe depend on the data,.999999

for the data, the result for decimals=2,3,4, all the result are the same.

Upvotes: 0

user6655984
user6655984

Reputation:

This is not an issue of rounding; it's an issue of display. The number 157.6 is not exactly representable in double precision arithmetics, its representative double-precision number is slightly less than 157.6. Python floats and NumPy floats differ in how to deal with this issue when displaying numbers in the console. Example:

x = 157.6
df = pd.DataFrame({'a': [x]})
y = df.iloc[0,0]

Now if you type x in the console, you get 157.6, but if you type y, you get 157.59999999999999. Yet, x == y is True. What's the deal?

Different types, that's all: x is a Python float, y is a NumPy float, since it came from a dataframe (pandas uses NumPy under the hood). Hence the cosmetic differences in appearances. The numbers are the same, there is nothing for rounding to do.

How to avoid the ugly 9s: use print(y), or in your example, print(Per_capita.iloc[0]). The print function will present the floats in a nice way, be they NumPy floats or Python floats.

Upvotes: 2

Mahesh
Mahesh

Reputation: 141

I believe you are solving coursera assignment. Just use following code if the answer should be number without any decimals:

Per_capita = int(Top15.mean())

If you need the answer to be 157.6 use:

Per_capita = Top15.mean()

Upvotes: 0

Related Questions