对列进行条件计数,并对结果进行平均计算 [英] Conditional Count over Columns with average calculations over results

查看:75
本文介绍了对列进行条件计数,并对结果进行平均计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这种结构的数据框

I have a dataframe with this structure

v1|v2|v3|
 2| 3| 4|
  | 5| 4|
 5| 1| 4|

我希望计算每列的所有4和5,然后将其除以该列所有条目的数目.之后,我想计算所有这些值的平均值.在这种情况下(1/2+1/3+3/3)/3=0.611

I waht to count all 4's and 5's for each column and divide it by the number of all entries of that column. Afterwards I want to calculate the mean of all these values. In this case (1/2+1/3+3/3)/3=0.611

sum over x(Count Vx if 4 or 5)/Count Vx(without missings)/x

到目前为止,我尝试过的是: df[df > 4 and df > 5].count(),但在这里我已经收到很多错误.有谁能走上正确的轨道?

What I have tried so far is: df[df > 4 and df > 5].count() but I already get a lot of errors here. Anyone who can bring on on the right track?

推荐答案

您可以尝试:

df.isin([4,5]).sum().div(df.count()).mean()

输出:

0.611111111111111

这篇关于对列进行条件计数,并对结果进行平均计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆