In pandas can you aggregate by mean and round that mean to the nearest int?

蹲街弑〆低调 提交于 2019-12-13 13:51:47

问题


So I have 169 columns which have been treated to leave 1=for yes and 0= for no, now I need to aggregate the 2 million rows by mean, and the round that results to the nearest int, how could I get that?

The image is just showing that the values per column are either 0 or 1


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/45451189/in-pandas-can-you-aggregate-by-mean-and-round-that-mean-to-the-nearest-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!