In pandas can you aggregate by mean and round that

2019-07-01 14:53发布

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

enter image description here

3条回答
不美不萌又怎样
2楼-- · 2019-07-01 15:42

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
查看更多
老娘就宠你
3楼-- · 2019-07-01 15:52

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
查看更多
唯我独甜
4楼-- · 2019-07-01 15:55

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

查看更多
登录 后发表回答