df
A
0 503.36
1 509.80
2 612.31
3 614.29
I want to round to nearest 5 in a new B column, using numpy if possible.
Output should be:
A B
0 503.36 505.00
1 509.80 510.00
2 612.31 610.00
3 614.29 615.00
To round a number up to the nearest 5:Call the math. ceil() method passing it the number divided by 5 . Multiply the result by 5 . The result of the calculation is the number rounded up to the nearest five.
Explanation: Here, we're rounding the value 1.5, which rounds upward to 2.0. Keep in mind that when you round a value that's exactly halfway between between two values, np. round will round to the nearest even value.
Since you mention numpy
np.around(df.A.values/5, decimals=0)*5
Out[31]: array([505., 510., 610., 615.])
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