str.contains
df["col"].str.contains("text") returns True for rows whose string contains the pattern, so df[df["col"].str.contains("text")] filters them. The pattern is a regular expression by default; add case=False for case-insensitive matches and na=False so missing values count as no match.
Syntax
Series.str.contains(pat, case=True, flags=0, na=None, regex=True)| Part | What it is |
|---|---|
pat | Substring or regular expression to search for |
case | False for case-insensitive matching |
na | Value to return for NaN entries; use False when filtering |
regex | False to treat pat as a literal string |
Examples
import pandas as pd
df = pd.DataFrame({"product": ["Blue Jeans", "Red Shirt", "blue hat", None]})
df[df["product"].str.contains("blue", case=False, na=False)]Result: product 0 Blue Jeans 2 blue hat
df[df["product"].str.contains("Jeans|Shirt", na=False)]Result: product 0 Blue Jeans 1 Red Shirt
The pipe in a regex means or.
s = pd.Series(["a+b", "ab"])
s.str.contains("a+b", regex=False)Result: 0 True 1 False dtype: bool
With regex=False the plus sign is matched literally.
Common mistakes
- ValueError: Cannot mask with non-boolean array containing NA: the column has NaN; pass na=False.
- No rows match because of case: pass case=False.
- Searching for a dot, plus, bracket or question mark matches too much because it is a regex: escape it or pass regex=False.
Practice str.contains
These drills check your answer on live data, free, no account needed.
- Search text with str.containsReturn the rows of `products` whose `item` contains the word organic in any capitalization.
- Tidy up text with strip and lowerThe emails in `signups` have stray spaces and mixed case. Return the `email` column as a Series with spaces removed from both ends and every letter in lowercase.
Questions
How do I check for several substrings?
Join them with |: str.contains("a|b|c"). To require all of them, chain conditions with &.
How do I do the opposite, rows not containing text?
Negate the mask: df[~df["col"].str.contains("text", na=False)].
What is the difference between contains and match?
contains looks for the pattern anywhere in the string; match requires it to match from the start; fullmatch requires the whole string to match.
Related
- Filter rowsHow to filter rows in a pandas DataFrame by one or several conditions using boolean masks, with the & | ~ operators, parentheses, isin, between and query.
- str.splitHow to split a string column in pandas with str.split: into separate columns with expand=True, limit the number of splits, take one part with str[0], regex.
- DataFrame.queryHow df.query works in pandas: conditions as a string with and, or, not, in, variables with @, column names with spaces, and when to use boolean masks instead.
- isinHow to use isin in pandas to keep rows whose value is in a list, set or Series, exclude with ~, check several columns at once and avoid the type mismatch.