Test whether pandas DataFrame Contains Particular Value in Python (Example Code)

In this post, I’ll demonstrate how to test whether a specific value is contained in a pandas DataFrame in the Python programming language.

Setting up the Example

import pandas as pd                            # Import pandas library to Python
my_df = pd.DataFrame({'A':[5, 7, 1, 2, 7],    # Construct example DataFrame
                      'B':[1, 1, 1, 1, 1]})
print(my_df)                                  # Display example DataFrame in console
#    A  B
# 0  5  1
# 1  7  1
# 2  1  1
# 3  2  1
# 4  7  1

Example: Searching for Values in pandas DataFrame

print(2 in my_df.values)
# True

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top