How to use a conditional statement based on DataFrame boolean value in pandas

If you want to check if any row of the DataFrame meets your conditions you can use .any() along with your condition . Example –

if ((df['column1']=='banana') & (df['colour']=='green')).any():

Example –

In [16]: df
Out[16]:
   A  B
0  1  2
1  3  4
2  5  6

In [17]: ((df['A']==1) & (df['B'] == 2)).any()
Out[17]: True

This is because your condition – ((df['column1']=='banana') & (df['colour']=='green')) – returns a Series of True/False values.

This is because in pandas when you compare a series against a scalar value, it returns the result of comparing each row of that series against the scalar value and the result is a series of True/False values indicating the result of comparison of that row with the scalar value. Example –

In [19]: (df['A']==1)
Out[19]:
0     True
1    False
2    False
Name: A, dtype: bool

In [20]: (df['B'] == 2)
Out[20]:
0     True
1    False
2    False
Name: B, dtype: bool

And the & does row-wise and for the two series. Example –

In [18]: ((df['A']==1) & (df['B'] == 2))
Out[18]:
0     True
1    False
2    False
dtype: bool

Now to check if any of the values from this series is True, you can use .any() , to check if all the values in the series are True, you can use .all() .

Leave a Comment