contains(self, pat, case=True, flags=0, na=None, regex=True)
Return boolean Series or Index based on whether a given pattern or regex is contained within a string of a Series or Index.
Character sequence or regular expression.
If True, case sensitive.
Flags to pass through to the re module, e.g. re.IGNORECASE.
Fill value for missing values. The default depends on dtype of the array. For object-dtype, numpy.nan
is used. For StringDtype
, pandas.NA
is used.
If True, assumes the pat is a regular expression.
If False, treats the pat as a literal string.
A Series or Index of boolean values indicating whether the given pattern is contained within the string of each element of the Series or Index.
Test if pattern or regex is contained within a string of a Series or Index.
Series.str.endswith
Same as startswith, but tests the end of string.
Series.str.startswith
Test if the start of each string element matches a pattern.
match
Analogous, but stricter, relying on re.match instead of re.search.
Returning a Series of booleans using only a literal pattern.
This example is valid syntax, but we were not able to check execution>>> s1 = pd.Series(['Mouse', 'dog', 'house and parrot', '23', np.NaN])
... s1.str.contains('og', regex=False) 0 False 1 True 2 False 3 False 4 NaN dtype: object
Returning an Index of booleans using only a literal pattern.
This example is valid syntax, but we were not able to check execution>>> ind = pd.Index(['Mouse', 'dog', 'house and parrot', '23.0', np.NaN])
... ind.str.contains('23', regex=False) Index([False, False, False, True, nan], dtype='object')
Specifying case sensitivity using :None:None:`case`
.
>>> s1.str.contains('oG', case=True, regex=True) 0 False 1 False 2 False 3 False 4 NaN dtype: object
Specifying :None:None:`na`
to be :None:None:`False`
instead of NaN
replaces NaN values with :None:None:`False`
. If Series or Index does not contain NaN values the resultant dtype will be bool
, otherwise, an :None:None:`object`
dtype.
>>> s1.str.contains('og', na=False, regex=True) 0 False 1 True 2 False 3 False 4 False dtype: bool
Returning 'house' or 'dog' when either expression occurs in a string.
This example is valid syntax, but we were not able to check execution>>> s1.str.contains('house|dog', regex=True) 0 False 1 True 2 True 3 False 4 NaN dtype: object
Ignoring case sensitivity using flags
with regex.
>>> import re
... s1.str.contains('PARROT', flags=re.IGNORECASE, regex=True) 0 False 1 False 2 True 3 False 4 NaN dtype: object
Returning any digit using regular expression.
This example is valid syntax, but we were not able to check execution>>> s1.str.contains('\\d', regex=True) 0 False 1 False 2 False 3 True 4 NaN dtype: object
Ensure :None:None:`pat`
is a not a literal pattern when regex
is set to True. Note in the following example one might expect only :None:None:`s2[1]`
and :None:None:`s2[3]`
to return :None:None:`True`
. However, '.0' as a regex matches any character followed by a 0.
>>> s2 = pd.Series(['40', '40.0', '41', '41.0', '35'])See :
... s2.str.contains('.0', regex=True) 0 True 1 True 2 False 3 True 4 False dtype: bool
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.strings.accessor.StringMethods.match
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them