Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round to nearest 1000 in pandas

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)? 
like image 704
Angelo Avatar asked Dec 23 '17 01:12

Angelo


People also ask

How do you round to the nearest 1000 in Python?

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.

How do you round to the nearest 500 in Python?

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 .


1 Answers

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
like image 134
grovina Avatar answered Oct 07 '22 17:10

grovina