Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round values in column from floats to ints using Python

I have a dataframe, df, where I would like to round values in column from floats to ints using Python

Data

    location    a site   b site   c site
    aa          4.9000   1.72222  0.29999
    bb          5.9000   6.72222  0.46999

Note I do not wish to round down on c site 0.29999

Desired

    location    a site   b site   c site
    aa          5        2        1
    bb          6        7        1

Doing

df[list("a site", "b site", "c site")] = df[list("a site", "b site", "c site")].astype(int)

Any suggestion is appreciated

like image 378
Lynn Avatar asked Oct 27 '25 14:10

Lynn


1 Answers

Use numpy.ceil to round up, and then convert the floats to integer using astype(int).

import numpy as np

df[["a site", "b site", "c site"]] = np.ceil(df[["a site", "b site", "c site"]]).astype(int)

>>> df

  location  a site  b site  c site
0       aa       5       2       1
1       bb       6       7       1
like image 164
Rodalm Avatar answered Oct 29 '25 03:10

Rodalm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!