Locate Specific Value in pandas DataFrame in Python (2 Examples)

This article shows how to locate a particular value in a pandas DataFrame in Python.

Introduction of Example Data

import pandas as pd                       # Import pandas
my_df = pd.DataFrame({'A':range(1, 7),    # Constructing a pandas DataFrame
                      'B':[1, 3, 5, 7, 9, 3],
                      'C':range(17, 11, - 1)})
print(my_df)
#    A  B   C
# 0  1  1  17
# 1  2  3  16
# 2  3  5  15
# 3  4  7  14
# 4  5  9  13
# 5  6  3  12

Example 1: Find pandas DataFrame Columns that Contain a Specific Value

print(my_df.isin([3]).any())              # Apply isin & any functions
# A     True
# B     True
# C    False
# dtype: bool

Example 2: Find pandas DataFrame Cells that Contain a Specific Value

print(my_df.isin([3]))                    # Apply isin function
#        A      B      C
# 0  False  False  False
# 1  False   True  False
# 2   True  False  False
# 3  False  False  False
# 4  False  False  False
# 5  False   True  False

Related Articles

Furthermore, you may want to have a look at the other articles on this website.

Leave a Reply

Your email address will not be published. Required fields are marked *

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