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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With