pandas 1.4.2

ParametersReturnsBackRef
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.

Parameters

pat : str

Character sequence or regular expression.

case : bool, default True

If True, case sensitive.

flags : int, default 0 (no flags)

Flags to pass through to the re module, e.g. re.IGNORECASE.

na : scalar, optional

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.

regex : bool, default True

If True, assumes the pat is a regular expression.

If False, treats the pat as a literal string.

Returns

Series or Index of boolean values

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.

See Also

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.

Examples

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`.

This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> s2 = pd.Series(['40', '40.0', '41', '41.0', '35'])
... s2.str.contains('.0', regex=True) 0 True 1 True 2 False 3 True 4 False dtype: bool
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.strings.accessor.StringMethods.match

Local connectivity graph

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


File: /pandas/core/strings/accessor.py#1089
type: <class 'function'>
Commit: