在 Pandas 中,您可以按平均值聚合并将平均值四舍五入到最近的整数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45451189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:09:19  来源:igfitidea点击:

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

pythonpandasanacondadata-analysis

提问by alexzaizar09

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?

所以我有 169 列被处理为留下 1= 表示是和 0= 表示否,现在我需要按平均值聚合 200 万行,以及结果到最近的整数的舍入,我怎么能得到呢?

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

该图像仅显示每列的值是 0 或 1

enter image description here

在此处输入图片说明

回答by Alexander

If datais your dataframe, you can get the mean of all the columns as integers simply with:

如果data是您的数据框,您可以简单地将所有列的平均值作为整数:

data.mean().astype(int)  # Truncates mean to integer, e.g. 1.95 = 1

or, as of version 0.17.0:

或者,从版本开始0.17.0

data.mean().round(0)  # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1

回答by Ryan

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!

使用 round() 函数。在 python3 的情况下,你不必导入数学库。检查 ceil 和 floor 分别向上和向下取整。对于 ceil 和 floor,您需要导入数学库。干杯和快乐的编码!

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

回答by Jinesh Bhandari

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())

您可以使用 python 的 round 函数来获得最接近整数的平均值,例如见下面 LotArea 的平均值被四舍五入到最接近的整数。avg_lot_size = round(home_data['LotArea'].mean())

if home_data['LotArea'].mean() gives value 100056.89 then avg_lot_size would be= 100057

如果 home_data['LotArea'].mean() 给出值 100056.89 那么 avg_lot_size 将是 = 100057