Pandas Series string | contains method
Start your free 7-days trial now!
Pandas Series str.contains(~) method checks whether or not each value of the source Series contains the specified substring or regex pattern.
Parameters
1. pat | string
The substring or regex to check for.
2. case | boolean | optional
If True, then check is case sensitive. By default, case=True.
3. flags | int | optional
A rule, which is available in Python's re module, to respect (e.g. re.IGNORECASE). By default, flags=0, that is, no rule is set.
4. na | scalar | optional
The value to replace NaN. By default, NaN are left as is.
5. regex | boolean | optional
Whether or not pat should be parsed as regex. By default, regex=True.
Return Value
A Series of booleans, where True indicates entries that contain the specified substring or regex pattern.
Examples
Substring search
To check for values in a Series that contain a specific substring:
s = pd.Series(["abc","abd","efg"])s.str.contains("ab", regex=False)
0 True1 True2 Falsedtype: bool
Using regex
Since regex=True by default, we can just provide the regex directly:
s = pd.Series(["a2a","a5a","aaa"])s.str.contains("\d")
0 True1 True2 Falsedtype: bool
Here, we are checking for values that contain a single digit.
Specifying na
We can fill NaN values by providing the na parameter like so:
s = pd.Series(["abc",np.nan])s.str.contains("a", na="**")
0 True1 **dtype: object