Jason Ching Yuk Jason Ching Yuk - 3 months ago 17
Python Question

Rounding up decimals Python

I am new to python pandas and I am having difficulties trying to round up all the values in the column as there is a white space between the decimal point and zero. For example,

Hi
21. 0
8. 0
52. 0
45. 0


I tried using my current code below, but it gave me:


invalid literal for float(): 21. 0


df.Hi.astype(float).round()

Answer

Try using replace on the string to replace all whitespace in the string before converting to a float:

df.Hi.str.replace(' ', '').astype(float).round()