🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

Series.str.contains()

AI & DATA SCIENCE // series-str-contains

series.str.contains() checks each string in a Series for a substring or regex pattern match, returning a boolean Series.

Syntax

series.str.contains(pat, case=True, na=None, regex=True)

Deep Dive Course

By default, pat is interpreted as a regex pattern, unlike str.replace(), which defaults to a literal match, so characters with special regex meaning need escaping, or regex=False, if you want a purely literal substring search. The na parameter controls what boolean value to use for missing, NaN, entries in the Series, since a NaN element has no string to search and would otherwise need special handling; case=False makes the match case-insensitive.

1Understanding Series.str.contains()

By default, pat is interpreted as a regex pattern, unlike str.replace(), which defaults to a literal match, so characters with special regex meaning need escaping, or regex=False, if you want a purely literal substring search. The na parameter controls what boolean value to use for missing, NaN, entries in the Series, since a NaN element has no string to search and would otherwise need special handling; case=False makes the match case-insensitive.

💡

Remember str.contains() defaults to treating its pattern as a regex, the opposite default from str.replace() — pass regex=False for a purely literal substring search if your pattern happens to include regex special characters you don't want interpreted.

editor.html
import pandas as pd

s = pd.Series(["apple pie", "banana bread", "apple juice"])
print(s.str.contains("apple"))
localhost:3000

2Practical Example

Here is a real-world application of Series.str.contains() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"email": ["alice@company.com", "bob@other.com"]})
work_emails = df[df["email"].str.contains("company")]
print(work_emails)
localhost:3000

3Best Practices

Follow these guidelines when working with Series.str.contains():

1. Combine str.contains() with boolean indexing to filter rows whose text matches a pattern or keyword

2. Pass case=False for case-insensitive matching instead of separately lowercasing both the Series and the search pattern first

3. Handle NaN values explicitly via the na parameter, or with an upfront fillna(), rather than letting them propagate as NaN into your boolean filter unexpectedly

⚠️

Tip: Remember str.contains() defaults to treating its pattern as a regex, the opposite default from str.replace() — pass regex=False for a purely literal substring search if your pattern happens to include regex special characters you don't want interpreted.

editor.html
import pandas as pd

s = pd.Series(["apple pie", "banana bread", "apple juice"])
print(s.str.contains("apple"))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series(["apple pie", "banana bread", "apple juice"])
print(s.str.contains("apple"))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"email": ["alice@company.com", "bob@other.com"]})
work_emails = df[df["email"].str.contains("company")]
print(work_emails)

Best Practices

  • Combine str.contains() with boolean indexing to filter rows whose text matches a pattern or keyword
  • Pass case=False for case-insensitive matching instead of separately lowercasing both the Series and the search pattern first
  • Handle NaN values explicitly via the na parameter, or with an upfront fillna(), rather than letting them propagate as NaN into your boolean filter unexpectedly

Interview Question

Why might str.contains() unexpectedly raise an error, or produce NaN in its result, when applied to a Series that has some missing (NaN) values?

Hint: Think about what str.contains() would need to search within for a missing element.

A NaN element has no actual string value to search a pattern against, so str.contains() can't meaningfully evaluate whether that missing entry contains anything — by default, it propagates that missing value forward as NaN in the resulting boolean Series rather than assuming True or False. This NaN-typed result can then cause problems if the boolean Series is used directly for indexing, since boolean indexing expects a purely True/False mask; passing an explicit na parameter, or filling the missing values beforehand, resolves that ambiguity deliberately instead of letting it surface unexpectedly.

Exercises

MediumPractice using Series.str.contains() in a real scenario.
View Solution
import pandas as pd

s = pd.Series(["apple pie", "banana bread", "apple juice"])
print(s.str.contains("apple"))

Frequently Asked Questions

Why might str.contains() unexpectedly raise an error, or produce NaN in its result, when applied to a Series that has some missing (NaN) values?

A NaN element has no actual string value to search a pattern against, so str.contains() can't meaningfully evaluate whether that missing entry contains anything — by default, it propagates that missing value forward as NaN in the resulting boolean Series rather than assuming True or False. This NaN-typed result can then cause problems if the boolean Series is used directly for indexing, since boolean indexing expects a purely True/False mask; passing an explicit na parameter, or filling the missing values beforehand, resolves that ambiguity deliberately instead of letting it surface unexpectedly.

Related Functions

series-str-replaceboolean-indexingre-module