![]() |
VOOZH | about |
The Series.str.contains() method is used to check whether each string value in a Pandas Series contains a given substring or pattern. It returns a Boolean Series (True/False) and is mainly used for filtering, searching, or flagging text data based on conditions.
This example checks whether each city name contains the substring "is".
0 True 1 False 2 True dtype: bool
Explanation:
Series.str.contains(pat, case=True, flags=0, na=None, regex=True)
Parameters:
Returns: A Boolean Series or Index
Example 1: This example uses a regex pattern to find names where "i" is followed by a lowercase letter.
0 True 1 True 2 True 3 False dtype: bool
Explanation: i[a-z] matches "ik" and "im" patterns in the strings
Example 2: This example performs a case-insensitive match to find "grape" in mixed-case text.
0 False 1 True 2 False dtype: bool
Explanation: flags=re.IGNORECASE allows matching "GRAPE" with "grape"
Example 3: This example shows how Series.str.contains() behaves when the Series contains missing values and how to handle them using the na parameter.
0 True 1 False 2 False 3 True dtype: bool
Explanation: