Counting the number of zeros in a row on a Pandas DataFrame?

Given a DataFrame, I would like to calculate the number of zeros in each row. How can I calculate it using Pandas?

Currently I have done this, it returns zeros indices

def is_blank(x): return x == 0 indexer = train_df.applymap(is_blank) 
+14
source share
3 answers

Use a logical comparison that will generate a logical df, then we can apply this to int, True will become 1, False will become 0, and then call count and pass param axis=1 to count in a row:

 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]: abc 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 

Violation of the above:

 In [65]: (df == 0) Out[65]: abc 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]: abc 0 0 1 1 1 1 1 1 2 1 0 1 3 0 1 1 4 0 0 1 

EDIT

as david pointed out, from astype to int not required, since Boolean types will be promoted to int when sum called, so this will simplify:

 (df == 0).sum(axis=1) 
+34
source

Here is another solution using 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) 
+3
source

You can count zeros per column using the following Python panda function. This may help someone who needs to calculate specific values ​​for each column.

 df.isin([0]).sum() 

Here df is the data frame, and the value we want to read is 0

0
source

Source: https://habr.com/ru/post/984179/


All Articles