Reputation: 4408
I have a dataframe, df, where I would like to round the value, 1.5 up to 2, while keeping the others the same (ex. 0.5)
Data
col1 col2
5 0.5
5 1.5
1.5 0.5
Desired
col1 col2
5 0.5
5 2
2 0.5
Doing
df['df'].apply(np.ceil)
However, this rounds the entire dataframe. I am still researching. Any advice is appreciated.
Upvotes: 0
Views: 32
Reputation: 900
This should also do well:
to_round = 1.5
for col in df.columns:
df.loc[df[col] == to_round, col] = round(to_round)
You could also feed it a list of numbers to round and then iterate over those as such:
to_round = [0.5, 1.5]
for col in df.columns:
for element in to_round:
df.loc[df[col] == element, col] = round(element)
Upvotes: 1
Reputation: 323386
In your case just replace
out = df.replace({1.5:2})
Out[80]:
col1 col2
0 5.0 0.5
1 5.0 2.0
2 2.0 0.5
Upvotes: 2