简体   繁体   English

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

[英]calculate mean or variance for subset of pandas DF column

I have a gigantic pandas DF with a bunch of columns in it. 我有一个巨大的熊猫DF,里面有一堆圆柱。 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: 每当我尝试使用如下代码时,我都会得到所有NaN:

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

or 要么

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

Thanks! 谢谢!

It's not totally clear what exactly you want to do, but it looks like what you mean is taking the per-row average of columns AD. 尚不清楚您到底想做什么,但您的意思似乎是取AD列的每行平均值。 In which case, you're just giving the wrong axis argument. 在这种情况下,您只是给出了错误的axis参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM