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.
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()
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')])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With