我的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,!=
相当于df.ne(0).sum(axis=1)
#alternatively (df!=0).sum(axis=1)
x 3
y 3
z 4
,轴上的总和= 1,使用:
qazxswpoi