1

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

  • 2
    Possible 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

1 Answers1

-1

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
mad_
  • 7,844
  • 2
  • 22
  • 37
  • I 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