how can i stop showing the decimal .0 after the whole number? python

Viewed 303

So, i entered code below to round a column to the nearest integer, though it still shows up with decimals. Eg. I want to round 62.040 to just 62 though once the code is entered it shows 62.0 in the output

data['Final'] = np.ceil(data['Total'].round())

3 Answers

This should work pretty ok data['Final'] = round(data['Total'])
This converts the decimal number to it nearest integer and the returned number doesn't have a decimal point

Note: I'm assuming that data is just dictionary that contains float numbers, since no more information was provided

after you round the column up, convert the column type to int

data=pd.DataFrame({
    'Total':[
        62.040, 63.040, 64.040,
        ],
    })
data['Final'] = data['Total'].round()
data['Final'] = data['Final'].astype(int) # or add this line to your code
print(data)
   Total  Final
0  62.04     62
1  63.04     63
2  64.04     64

Take your decimal value and pass it to the int function for example:

z = 123.45678
x = 1.0
y = 1.6
a = int(z)#a will be 123
b = int(x)#b will be 1
c = int(y)#c will be 1

However, if the number is 1.6 this will be converted to a 1 chopping the decimal off (because int always rounds down). So you should round to 0dp first.

z = 123.45678
x = 1.0
y = 1.6
a = int(round(z,0))#a will be 123
b = int(round(x,0))#b will be 1
c = int(round(y,0))#c will be 2

So for you this will work:

data['final'] = int( round(np.ceil(data['Total']),0) )
Related