I am having a dataframe column and want to round it. If the value is equal to 0.5 it is getting rounded as 0, but i want it to be 1 if the value is greater than or equal to 0.5. Could someone please help
-
2Possible duplicate of [How to round float 0.5 up to 1.0, while still rounding 0.45 to 0.0, as the usual school rounding?](https://stackoverflow.com/questions/43851273/how-to-round-float-0-5-up-to-1-0-while-still-rounding-0-45-to-0-0-as-the-usual) – yatu Mar 04 '19 at 19:41
-
have you tried `df.round(0)`? – mad_ Mar 04 '19 at 19:42
-
@mad_ yes but rounding to zero – Mebin Thomas Mar 04 '19 at 19:45
2 Answers
As mentionned by @ALoolz in a comment, pandas (and python in general) is using a rounding to minimize bias when summing different elements, called Rounding half to even.
As Wikipedi says:
Rounding half to even
A tie-breaking rule without positive/negative bias and without bias toward/away from zero is round half to even. By this convention, if the fractional part of
x
is0.5
, theny
is the even integer nearest tox
. Thus, for example,+23.5
becomes+24
, as does+24.5
; however,−23.5
becomes−24
, as does−24.5
. This function minimizes the expected error when summing over rounded figures, even when the inputs are mostly positive or mostly negative, provided they are neither mostly even nor mostly odd.This variant of the round-to-nearest method is also called convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, odd–even rounding,[6] or bankers' rounding.
This is the default rounding mode used in IEEE 754 operations for results in binary floating-point formats, and the more sophisticated mode[clarification needed] used when rounding to significant figures.
By eliminating bias, repeated addition or subtraction of independent numbers, as in a one-dimensional random walk, will give a rounded result with an error that tends to grow in proportion to the square root of the number of operations rather than linearly.
However, this rule distorts the distribution by increasing the probability of evens relative to odds. Typically this is less important[citation needed] than the biases that are eliminated by this method.
In your case, if you want to use the rounding used "in school" called Rounding half up, you could apply a function that adds 0.5 and then truncate: lambda x: int(x + 0.5)
So, you should do when rounding your DataFrame:
df_rounding_half_up = df.applymap(lambda x: int(x + 0.5))
... while Python-default round up to even would be something like:
df_rounding_half_to_even = df.applymap(lambda x: int(round(x, 0)))

- 11,549
- 7
- 68
- 107
There seems to be an issue in pandas.round
function which does not round 0.5
to 1
. In that case you could use built-in round
with applymap
import pandas as pd
import numpy as np
def getRound(x):
return(round(x))
df = pd.DataFrame(np.random.random([3, 3]),
columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
df
will look like this
A B C
first 0.474011 0.082135 0.476545
second 0.313154 0.265458 0.523410
third 0.057491 0.141635 0.037582
Change one value to be 0.5
df['A'][1]=0.5
Apply lambda function
df.applymap(getRound)
Output:
A B C
first 0.0 0.0 0.0
second 1.0 0.0 1.0
third 0.0 0.0 0.0

- 8,121
- 2
- 25
- 40
-
1I wouldn't say it's an `issue`. It's a conscious decision to have an unbiased method of rounding, see [Round half to even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) – ALollz Mar 04 '19 at 20:24
-
Correct link to the article is now: [Rounding half to even](https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even) – Jean-Francois T. Nov 29 '22 at 03:08