在大 pandas 中,您可以通过均值聚合并将其四舍五入到最接近的整数吗? [英] In pandas can you aggregate by mean and round that mean to the nearest int?
本文介绍了在大 pandas 中,您可以通过均值聚合并将其四舍五入到最接近的整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我有169列已被处理为保留1 =表示是,0 =表示否,现在我需要平均合计200万行,以及 结果到最接近的整数,我怎么能得到呢?
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?
图像仅显示每列的值为0或1
The image is just showing that the values per column are either 0 or 1
推荐答案
如果data
是您的数据框,则可以使用以下方法简单地获取所有列的平均值:
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
或者,从版本0.17.0
开始:
data.mean().round(0) # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1
这篇关于在大 pandas 中,您可以通过均值聚合并将其四舍五入到最接近的整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文