Examples
Returning a Series of booleans using only a literal pattern.
import numpy as np
import pandas as pd
s1 = pd.Series(['Tiger', 'fox', 'house and men', '20', np.NaN])
s1.str.contains('ox', regex=False)
Returning an Index of booleans using only a literal pattern.
ind = pd.Index(['Tiger', 'fox', 'house and men', '20.0', np.NaN])
ind.str.contains('20', regex=False)
Specifying case sensitivity using case.
s1.str.contains('oX', case=True, regex=True)
Specifying na to be False instead of NaN replaces NaN values with False. If Series or Index does not contain
NaN values the resultant dtype will be bool, otherwise, an object dtype.
s1.str.contains('ox', na=False, regex=True)
Returning ‘house’ or ‘fox’ when either expression occurs in a string.
s1.str.contains('house|fox', regex=True)
Ignoring case sensitivity using flags with regex.
import re
s1.str.contains('MEN', flags=re.IGNORECASE, regex=True)
Returning any digit using regular expression.
s1.str.contains('\d', regex=True)
Ensure pat is a not a literal pattern when regex is set to True. Note in the following example one might
expect only s2[1] and s2[3] to return True. However, ‘.0’ as a regex matches any character followed by a 0.
s2 = pd.Series(['60', '60.0', '61', '61.0', '45'])
s2.str.contains('.0', regex=True)