Home > Software engineering >  How to count number of times a condition is met across a row?
How to count number of times a condition is met across a row?

Time:01-06

I have the following DataFrame

df = pd.DataFrame({'name': ['steve', 'josh', 'mike'],
                   'one': [0.4, 0.8, 0.2],
                   'two': [1, 0.1, 0.1],
                   'three': [1, 1, 0.99]})

that looks like this

name   one   two  three  
steve  0.4   1    1
josh   0.8   0.1  1
mike   0.2   0.1  0.99

I would like to add a new column to this table that counts how many times a specific condition is met across each row. If a value is not equal to 1, then count it.

The result should look like this

name   one   two  three  sums
steve  0.4   1    1      1
josh   0.8   0.1  1      2
mike   0.2   0.1  0.99   3

I'm really unsure how to approach this.

CodePudding user response:

My translation of "If a value is not equal to 1, then count it" (using select_dtypes to consider only numeric columns):

df['sums'] = df.select_dtypes('number').ne(1).sum(axis=1)

print(df)

    name  one  two  three  sums
0  steve  0.4  1.0   1.00     1
1   josh  0.8  0.1   1.00     2
2   mike  0.2  0.1   0.99     3

CodePudding user response:

You can use .ne (not equal) and sum along rows using the argument axis=1:

df['sums'] = df[['one','two','three']].ne(1).sum(axis=1)

CodePudding user response:

another angle to look from

df['sums']=(df[['one','two','three']]!=1).T.sum()
#or    
df['sums']=(df[['one','two','three']]!=1).sum(axis=1)

output:

name   one  two  three  sums
                        
steve  0.4  1.0   1.00     1
josh   0.8  0.1   1.00     2
mike   0.2  0.1   0.99     3
  •  Tags:  
  • Related