I don't know of a standard function in Python, but this works for me:
Python 3
def myround(x, base=5):
return base * round(x/base)
It is easy to see why the above works. You want to make sure that your number divided by 5 is an integer, correctly rounded. So, we first do exactly that (round(x/5)), and then since we divided by 5, we multiply by 5 as well.
I made the function more generic by giving it a base parameter, defaulting to 5.
Python 2
In Python 2, float(x) would be needed to ensure that / does floating-point division, and a final conversion to int is needed because round() returns a floating-point value in Python 2.
def myround(x, base=5):
return int(base * round(float(x)/base))
Answer from Alok Singhal on Stack OverflowI don't know of a standard function in Python, but this works for me:
Python 3
def myround(x, base=5):
return base * round(x/base)
It is easy to see why the above works. You want to make sure that your number divided by 5 is an integer, correctly rounded. So, we first do exactly that (round(x/5)), and then since we divided by 5, we multiply by 5 as well.
I made the function more generic by giving it a base parameter, defaulting to 5.
Python 2
In Python 2, float(x) would be needed to ensure that / does floating-point division, and a final conversion to int is needed because round() returns a floating-point value in Python 2.
def myround(x, base=5):
return int(base * round(float(x)/base))
For rounding to non-integer values, such as 0.05:
def myround(x, prec=2, base=.05):
return round(base * round(float(x)/base),prec)
I found this useful since I could just do a search and replace in my code to change "round(" to "myround(", without having to change the parameter values.
Videos
Im trying to round values like 2.67 to 2.50 and 1.75 to 2.00. How can i do this?
Since you mention numpy
np.around(df.A.values/5, decimals=0)*5
Out[31]: array([505., 510., 610., 615.])
You can use:
df['B'] = df.div(5).round(0) * 5
Or as @piRSquared states:
df['B'] = df['A'].mul(2).round(-1).div(2)
Output:
A B
0 503.36 505.0
1 509.80 510.0
2 612.31 610.0
3 614.29 615.0
I have a dataframe df:
df = pd.DataFrame({"volume": [0.3300, 5.600, 64.0915, 1.730000, 4.123000]})| volume |
|---|
| 0.3300 |
| 5.600 |
| 64.0915 |
| 1.730000 |
| 4.123000 |
I also have a non-exhausting dict di:
di = {
0.5: 6.26,
1.0: 6.28,
1.5: 6.36,
2.0: 6.46,
2.5: 6.56,
3.0: 6.66,
3.5: 6.76,
4.0: 6.86,
4.5: 6.96,
5.0: 6.98,
5.5: 7.15
...
}I need to create a new column ["map"] where I map di to df["volume"].
df["map"] = df["volume"].map(di)
but for that I need to round up each number in df["volume"] to the next 0.5, so the values should look like:
| volume | volume_round_up |
|---|---|
| 0.3300 | 0.5 |
| 5.600 | 6.0 |
| 64.0915 | 64.5 |
| 1.730000 | 2.0 |
| 4.123000 | 4.5 |
How can I do this in a vectorized way?
Python's builtin round function can be used to round a number to a certain number of decimal places. By multiplying the number by 2, rounding it to no decimal places and dividing it again, we can round a number to the nearest 0.5:
num = 3.46
result = round(num * 2) / 2
print(result) # 3.5
As a function this is:
def to_nearest_half(num):
return round(num * 2) / 2
The function can also be generalised to any decimal:
def to_nearest(num, decimal):
return round(num / decimal) * decimal
That would be
def roundhalf(x: float):
return round(x*2)/2
Can be tested with
for x in [100.4, 100.6, 100.9]:
print(roundup(x))
The output is indeed
100.5
100.5
101.0
The math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
I know this answer is for a question from a while back, but if you don't want to import math and you just want to round up, this works for me.
>>> int(21 / 5)
4
>>> int(21 / 5) + (21 % 5 > 0)
5
The first part becomes 4 and the second part evaluates to "True" if there is a remainder, which in addition True = 1; False = 0. So if there is no remainder, then it stays the same integer, but if there is a remainder it adds 1.