How to check if a value is True in pandas columns?

Checking if any pandas column values are True

We can easily find out whether any values in our Pandas DataFrame column value answers a specific condition using the any() method of a pandas Series. To find our whether any value in your column is greater than a constant, use the following code:

(your_df['your_column'] >= constant).any()

Creating example data

We will start by creating a simple dataset:

import pandas as pd
language = ['R', 'Java', 'Python', 'R', 'Java']
salary = [159.0, 199.0, 102.0, 122.0, 154.0]
data = dict(language = language, salary = salary)
test_df = pd.DataFrame(data=data)
test_df.head()

This will return the following data:

languagesalary
0R159.0
1Java199.0
2Python102.0
3R122.0
4Java154.

Check if any column value is True

We can check whether our Dataset contains Python or R related entries. We’ll specifically check the language Series as shown below:

(test_df['language'].isin (['Python', 'R']))

This will return a boolean array:

0     True
1    False
2     True
3     True
4    False
Name: language, dtype: bool

To check whether each of the values is True we use the Series any() method:

(test_df['language'].isin (['Python', 'R'])).any()

This will obviously return a True result.

Check if values are greater or smaller than a constant in a Series

Let’s check for example, if any of our candidate salaries is higher than 200K. Also here we will use the any() Series method to find any True results.

print((test_df['salary'] >=200).any())

As no salaries are greater than 199K, this will return a False result.

Dealing with multiple conditions

We can check if our DataFrame column values answer several conditions. We will start by defining a more complex boolean condition:

cond = (test_df['language'] == 'Python') & (test_df['salary'] > 100)

We can then slice the DataFrame and find whether at least one of the rows answers the condition:

test_df[cond].any().any()

The result is True.