Python 通过 Pandas DataFrame 计算每行零的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29229600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting number of zeros per row by Pandas DataFrame?
提问by erogol
Given a DataFrame I would like to compute number of zeros per each row. How can I compute it with Pandas?
给定一个 DataFrame,我想计算每行的零数。我如何用 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)
采纳答案by EdChum
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 countand pass param axis=1to count row-wise:
使用将产生布尔 df 的布尔比较,然后我们可以将其转换为 int,True 变为 1,False 变为 0,然后调用count并传递 paramaxis=1以按行计数:
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
Breaking the above down:
分解上述内容:
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
EDIT
编辑
as pointed out by david the astypeto intis unnecessary as the Booleantypes will be upcasted to intwhen calling sumso this simplifies to:
正如 david 所指出的,astypetoint是不必要的,因为在调用时Boolean类型将被向上转换int,sum因此这简化为:
(df == 0).sum(axis=1)
回答by Dov Grobgeld
Here is another solution using apply()and value_counts().
这是使用apply()and 的另一种解决方案value_counts()。
df = pd.DataFrame({'a':[1,0,0,1,3], 'b':[0,0,1,0,1], 'c':[0,0,0,0,0]})
df.apply( lambda s : s.value_counts().get(0,0), axis=1)
回答by Dov Grobgeld
You can count the zeros per column using the following function of python pandas. It may help someone who needs to count the particular values per each column
您可以使用 python pandas 的以下函数计算每列的零。它可能有助于需要计算每列特定值的人
df.isin([0]).sum()
Here df is the dataframe and the value which we want to count is 0
这里 df 是数据帧,我们要计数的值为 0

![Python 数据类型“datetime64[ns]”和“<M8[ns]”之间的区别?](/res/img/loading.gif)