Anirudh Maru
Anirudh Maru

Reputation: 31

How can I count the number of rows that meet multiple criteria on python?

I am trying to count the number of rows in a dataframe where the value of two columns in the dataframe equal to 'clear'. The code I have is:

pass_both_checks = len(merged_outer(([merged_outer['result_x'] == 'clear']) & [merged_outer['result_y'] == 'clear']))

where 'merged_outer' is the dataframe and I want the result to be the count of all rows where column 'result_x' and column 'result_y' equals 'clear'.

However, I am getting an error:

TypeError: unsupported operand type(s) for &: 'list' and 'list'

How do I fix this? If I insert just one condition in the code for pass_both_checks, it seems to work fine.

Upvotes: 2

Views: 2374

Answers (3)

jezrael
jezrael

Reputation: 862671

For filtering by boolean indexing add () around conditions:

pass_both_checks = len(merged_outer[(merged_outer['result_x'] == 'clear') & 
                                    (merged_outer['result_y'] == 'clear')])

Or use sum for count Trues:

pass_both_checks = ((merged_outer['result_x'] == 'clear') & 
                    (merged_outer['result_y'] == 'clear')).sum()

Or compare both columns and test if all Trues per rows by DataFrame.all:

pass_both_checks = (merged_outer[['result_x', 'result_y']] == 'clear').all(axis=1).sum()

Upvotes: 6

Epsi95
Epsi95

Reputation: 9047

you can get row wise check then get only the True

import pandas as pd
df = pd.DataFrame([['a', 'clear'], ['b', 'c'], ['clear', 'clear'], ['clear', 'clear']], columns=['A', 'B'])

(df[['A', 'B']]=='clear').all(axis=1).value_counts()[True] #2

Upvotes: 1

Keyser Soze
Keyser Soze

Reputation: 272

I think it's just a syntax problem, you have to replace few parentheses with brackets as following :

pass_both_checks = len(merged_outer.loc[(merged_outer['result_x'] == 'clear']) & (merged_outer['result_y'] == 'clear')])

Upvotes: 2

Related Questions