Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Try with np.ceil round

df.update(df.select_dtypes(np.number).apply(np.ceil))
df
  location  asite  bsite  csite
0       aa    5.0    2.0    1.0
1       bb    6.0    7.0    1.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading