Rounding numbers in Pandas

Question:

How do you round a column to 1dp?

df value 
0 0.345
1 0.45
2 0.95

Expected Output

0.3
0.5
1.0

All the below give the wrong answers:

df.value.round(1)
df.value.apply(lambda x:round(x,1))
Asked By: asd

||

Answers:

As far as floating-point operations are concerned, Python behaves like many popular languages including C and Java. Many numbers that can be written easily in decimal notation cannot be expressed exactly in binary floating-point. The decimal value of 0.95 is actually 0.94999999999999996

To check:

from decimal import Decimal
Decimal(0.95)

Output

Decimal('0.9499999999999999555910790149937383830547332763671875')

Here’s a useful "normal" rounding function which takes in number n, and returns n to specified decimal places:

import math

def normal_round(n, decimal):
    exp = n * 10 ** decimal
    if abs(exp) - abs(math.floor(exp)) < 0.5:
        return math.floor(exp) / 10 ** decimal
    return math.ceil(exp) / 10 ** decimal

Original df

df = pd.DataFrame({'value': [0.345, 0.45, 0.95]})
    value
0   0.345
1   0.450
2   0.950

code

df['value'] = df['value'].apply(lambda x: normal_round(x, 1))

Output df

    value
0   0.3
1   0.5
2   1.0

More examples on rounding floating point:

df = pd.DataFrame({'value': [0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95]})
df['value_round'] = df['value'].apply(lambda x: round(x, 1))
df['value_normal_round'] = df['value'].apply(lambda x: normal_round(x, 1))

Output

    value   value_round value_normal_round
0   0.15    0.1         0.2
1   0.25    0.2         0.3
2   0.35    0.3         0.4
3   0.45    0.5         0.5
4   0.55    0.6         0.6
5   0.65    0.7         0.7
6   0.75    0.8         0.8
7   0.85    0.8         0.9
8   0.95    0.9         1.0
Answered By: perpetualstudent