pandas 计算熊猫 DF 列子集的均值或方差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27235354/
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-13 22:44:08  来源:igfitidea点击:

calculate mean or variance for subset of pandas DF column

pythonpandasmean

提问by user3786999

I have a gigantic pandas DF with a bunch of columns in it. I want to calculate mean and variance for a subset of three columns. Is there an easy way to do this without creating a whole new dataframe? I get all NaN whenever I try to use something like this:

我有一个巨大的Pandas DF,里面有一堆柱子。我想计算三列子集的均值和方差。有没有一种简单的方法可以在不创建全新数据框的情况下做到这一点?每当我尝试使用这样的东西时,我都会得到所有 NaN:

DF['means']=np.mean(DF.A, DF.B, DF.C)

or

或者

DF['means']=DF[['A','B','C','D']].mean(axis=0)

Thanks!

谢谢!

回答by Mark Whitfield

It's not totally clear what exactly you want to do, but it looks like what you mean is taking the per-rowaverage of columns A-D. In which case, you're just giving the wrong axis argument.

不完全清楚您到底想做什么,但看起来您的意思是取AD 列的每行平均值。在这种情况下,您只是给出了错误的轴参数。

DF['means']=DF[['A','B','C','D']].mean(axis=1)should work fine.

DF['means']=DF[['A','B','C','D']].mean(axis=1)应该工作正常。