计算pandas中每行具有某些值的列数 [英] Count number of columns with some values for each row in pandas

查看:57
本文介绍了计算pandas中每行具有某些值的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框,数据:

I have dataframe like this, data:

Site code    Col1  Col2  Col3
A5252        24    53     NaN
A5636        36    NaN    NaN
A4366        NaN   NaN    NaN
A7578        42    785    24

而且我想计算一些具有某些值的列,但没有 NaN.期望输出:

And I want to count a number of columns with some value, but none NaN. Desired output:

 Site code   Col1  Col2  Col3  Count
    A5252     24    53     NaN    2
    A5636     36    NaN    NaN    1
    A4366     NaN   NaN    NaN    0
    A7578     42    785    24     3

与此相反的东西:df = data.isnull().sum(axis=1)

Something oposite to this: df = data.isnull().sum(axis=1)

推荐答案

需要改变 isnullnotnull:

#if first columns is not index, set it
data = data.set_index('Site code')
data['Count'] = data.notnull().sum(axis=1)

或者使用函数DataFrame.count:

Or use function DataFrame.count:

data = data.set_index('Site code')
data['Count'] = data.count(axis=1)
print (data)
           Col1   Col2  Col3  Count
Site code                          
A5252      24.0   53.0   NaN      2
A5636      36.0    NaN   NaN      1
A4366       NaN    NaN   NaN      0
A7578      42.0  785.0  24.0      3

通过 loc 选择列的另一种解决方案(站点代码是列,不是index):

print (data.loc[:, 'Col1':])
   Col1   Col2  Col3
0  24.0   53.0   NaN
1  36.0    NaN   NaN
2   NaN    NaN   NaN
3  42.0  785.0  24.0

data['Count'] = data.loc[:, 'Col1':].count(axis=1)
print (data)
  Site code  Col1   Col2  Col3  Count
0     A5252  24.0   53.0   NaN      2
1     A5636  36.0    NaN   NaN      1
2     A4366   NaN    NaN   NaN      0
3     A7578  42.0  785.0  24.0      3

来自 Jon Clements - 使用 过滤器:

Another nice idea from Jon Clements - use filter:

data['Count'] = data.filter(regex="^Col").count(axis=1)
print (data)

  Site code  Col1   Col2  Col3  Count
0     A5252  24.0   53.0   NaN      2
1     A5636  36.0    NaN   NaN      1
2     A4366   NaN    NaN   NaN      0
3     A7578  42.0  785.0  24.0      3

这篇关于计算pandas中每行具有某些值的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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