Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round to nearest 5 with numpy

Tags:

python

pandas

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
like image 967
Tie_24 Avatar asked Mar 14 '18 15:03

Tie_24


People also ask

How do you round to the nearest 5 in Python?

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.

Does NP round round up or down?

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.


1 Answers

Since you mention numpy

np.around(df.A.values/5, decimals=0)*5
Out[31]: array([505., 510., 610., 615.])
like image 155
BENY Avatar answered Sep 28 '22 04:09

BENY