Pandas
DataFrame
Python
Data Analysis
Data Science

How to check if a pandas column contains a value

Codemia Knowledge Hub ·

Compare the column to the value, then reduce the boolean result with .any(): df["col"].eq(value).any(). Use isin when you are checking against several values, and .str.contains(...) when "contains" means a substring inside text.

Exact value

python
1import pandas as pd
2
3s = pd.Series(["apple", "banana", "cherry"])
4contains_cherry = s.eq("cherry").any()
5print(contains_cherry)

s == "cherry" also works, but .eq(...) is explicit and chains well with other methods.

Any of several values

To know whether the column contains any value from a set, use isin. It is cleaner than chaining several equality checks.

python
1import pandas as pd
2
3s = pd.Series(["apple", "banana", "cherry"])
4result = s.isin(["banana", "orange"]).any()
5print(result)

Substring in a text column

If the column holds strings and you mean substring containment, use .str.contains(...):

python
1import pandas as pd
2
3s = pd.Series(["alpha", "beta", "alphabet"])
4contains_substring = s.str.contains("alpha", na=False).any()
5print(contains_substring)

Pass na=False when the column may have missing values, so nulls do not produce awkward edge cases in the boolean result.

A column in a DataFrame

The same patterns apply to a named column:

python
1import pandas as pd
2
3df = pd.DataFrame({"fruit": ["apple", "banana", "cherry"]})
4print(df["fruit"].eq("banana").any())

Why .any() is needed

The comparison on its own returns a boolean Series, one value per row, not a single boolean:

python
print(df["fruit"].eq("banana"))

That Series tells you which rows match. .any() collapses it to the yes-or-no answer: did at least one row match? If you need the matching rows themselves, keep the boolean mask instead of collapsing it.

Missing values and type mismatches

For exact equality checks, pandas usually handles nulls well enough. For string methods such as .str.contains(...), missing values can interfere unless you pass na=False or clean the column first.

Types matter too: searching for the integer 5 in a string column is not the same as searching for the string "5".


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design