Reputation: 31
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
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 True
s:
pass_both_checks = ((merged_outer['result_x'] == 'clear') &
(merged_outer['result_y'] == 'clear')).sum()
Or compare both columns and test if all True
s per rows by DataFrame.all
:
pass_both_checks = (merged_outer[['result_x', 'result_y']] == 'clear').all(axis=1).sum()
Upvotes: 6
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
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