In pandas can you aggregate by mean and round that mean to the nearest int?
Use the round() function. In case of python3 then you don't have to import the math lib. Check out ceil and floor to round up and down respectively. For ceil and floor you need to import the math lib. Cheers and happy coding!
import math
mean = 8.907
print(round(mean)) # results in 9
print(math.floor(mean)) # results in 8
print(math.ceil(mean)) # results in 9
If data
is your dataframe, you can get the mean of all the columns as integers simply with:
data.mean().astype(int) # Truncates mean to integer, e.g. 1.95 = 1
or, as of version 0.17.0
:
data.mean().round(0) # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1
You can use python's round function to get mean value in nearest integer, for example see below mean of LotArea was rounded to nearest int. avg_lot_size = round(home_data['LotArea'].mean())
if home_data['LotArea'].mean() gives value 100056.89 then avg_lot_size would be= 100057