通过Pandas DataFrame计算每行零的数量?(Counting number of zeros per row by Pandas DataFrame?)

编程入门 行业动态 更新时间:2024-10-27 22:20:46
通过Pandas DataFrame计算每行零的数量?(Counting number of zeros per row by Pandas DataFrame?)

给定一个DataFrame,我想计算每行的零个数。 我如何用Pandas进行计算?

这是我现在所做的,这返回了零的索引

def is_blank(x): return x == 0 indexer = train_df.applymap(is_blank)

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并传递param axis=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

打破以上:

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

编辑

正如david所指出的那样, astype为int是不必要的,因为在调用sum时Boolean类型将被升级为int ,所以这简化为:

(df == 0).sum(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

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 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)

更多推荐

本文发布于:2023-04-28 01:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   DataFrame   Pandas   Counting   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!