通过Pandas DataFrame计数每行零个数字? [英] Counting number of zeros per row by Pandas DataFrame?
本文介绍了通过Pandas DataFrame计数每行零个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个DataFrame,我想计算每行的零个数.如何使用Pandas进行计算?
Given a DataFrame I would like to compute number of zeros per each row. How can I compute it with Pandas?
这是我目前所做的,返回的索引为零
This is presently what I ve done, this returns indices of zeros
def is_blank(x):
return x == 0
indexer = train_df.applymap(is_blank)
推荐答案
使用布尔比较将产生布尔df,然后可以将其强制转换为int,True变为1,False变为0,然后调用count
并传递参数axis=1
进行逐行计数:
Use a boolean comparison which will produce a boolean df, we can then cast this to int, True becomes 1, False becomes 0 and then call count
and pass param axis=1
to count row-wise:
In [56]:
df = pd.DataFrame({'a':[1,0,0,1,3], 'b':[0,0,1,0,1], 'c':[0,0,0,0,0]})
df
Out[56]:
a b c
0 1 0 0
1 0 0 0
2 0 1 0
3 1 0 0
4 3 1 0
In [64]:
(df == 0).astype(int).sum(axis=1)
Out[64]:
0 2
1 3
2 2
3 2
4 1
dtype: int64
打破以上:
In [65]:
(df == 0)
Out[65]:
a b c
0 False True True
1 True True True
2 True False True
3 False True True
4 False False True
In [66]:
(df == 0).astype(int)
Out[66]:
a b c
0 0 1 1
1 1 1 1
2 1 0 1
3 0 1 1
4 0 0 1
编辑
不需要astype
到int
,因为在调用sum
时Boolean
类型将被转换为int
,因此简化为:
as pointed out by david the astype
to int
is unnecessary as the Boolean
types will be upcasted to int
when calling sum
so this simplifies to:
(df == 0).sum(axis=1)
这篇关于通过Pandas DataFrame计数每行零个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文