I've searched the pandas documentation and cookbook recipes and it's clear you can round to the nearest decimal place easily using dataframe.columnName.round(decimalplace)
.
How do you do this with larger numbers?
Example, I have a column of housing prices and I want them rounded to the nearest 10000 or 1000 or whatever.
df.SalesPrice.WhatDoIDo(1000)?
Use the round() function to round a number to the nearest 1000, e.g. result = round(num, -3) . When the round() function is called with a second argument of -3 , it rounds to the closest multiple of one thousand.
Round a number Up to the nearest 500 in Python #Call the math. ceil() method passing it the number divided by 500 . Multiply the result by 500 .
Function round does accept negative values for cases in which you want to specify precision to the left of the decimal point:
dataframe.columnName.round(-3)
Example:
>>> pd.Series([1, 500, 500.1, 999, 1500, 1501, 946546]).round(-3)
0 0.0
1 0.0
2 1000.0
3 1000.0
4 2000.0
5 2000.0
6 947000.0
dtype: float64
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