[英]Counting Rows with Condition in Pandas
我的熊猫数据框如下所示:
a b c d e
x 2 4 5 0 0
y 0 3 8 9 0
z 2 3 5 0 1
我想计算值!= 0的行。我已经尝试过这一行,但是输出不符合我的期望:
df.sum(axis=1)
x 11
y 20
z 11
我期望的输出:
x 3
y 3
z 4
我怎样才能做到这一点?
使用.ne()
过滤掉0,它等于!=
并在轴= 1上求和,请使用:
df.ne(0).sum(axis=1)
#alternatively (df!=0).sum(axis=1)
x 3
y 3
z 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.