maxframe.dataframe.Series.str.contains#
- Series.str.contains(pat, case: bool = True, flags: int = 0, na=<no_default>, regex: bool = True)#
测试模式或正则表达式是否包含在 Series 或 Index 的字符串中。
根据给定的模式或正则表达式是否包含在 Series 或 Index 的字符串中返回布尔值 Series 或 Index。
- 参数:
pat (str) -- 字符序列或正则表达式。
case (bool, default True) -- 如果为 True,则区分大小写。
flags (int, default 0 (no flags)) -- 传递给 re 模块的标志,例如 re.IGNORECASE。
na (scalar, optional) -- Fill value for missing values. The default depends on dtype of the array. For the
"str"dtype,Falseis used. For object dtype,numpy.nanis used. For the nullableStringDtype,pandas.NAis used.regex (bool, default True) -- 如果为 True,则假定 pat 是正则表达式。如果为 False,则将 pat 视为字面字符串。
- 返回:
一个布尔值 Series 或 Index,表示给定模式是否包含在 Series 或 Index 的每个元素的字符串中。
- 返回类型:
参见
match类似的,但更严格,依赖于 re.match 而不是 re.search。
Series.str.startswith测试每个字符串元素的开头是否与模式匹配。
Series.str.endswith与 startswith 相同,但测试字符串的结尾。
示例
仅使用字面模式返回一个布尔值 Series。
>>> import maxframe.tensor as mt >>> import maxframe.dataframe as md >>> s1 = md.Series(["Mouse", "dog", "house and parrot", "23", mt.nan]) >>> s1.str.contains("og", regex=False).execute() 0 False 1 True 2 False 3 False 4 False dtype: bool
仅使用字面模式返回一个布尔值 Index。
>>> ind = md.Index(["Mouse", "dog", "house and parrot", "23.0", mt.nan]) >>> ind.str.contains("23", regex=False).execute() array([False, False, False, True, False])
使用 case 指定是否区分大小写。
>>> s1.str.contains("oG", case=True, regex=True).execute() 0 False 1 False 2 False 3 False 4 False dtype: bool
当字符串中出现任一表达式时返回 'house' 或 'dog'。
>>> s1.str.contains("house|dog", regex=True).execute() 0 False 1 True 2 True 3 False 4 False dtype: bool
使用带有 flags 的正则表达式忽略大小写。
>>> import re >>> s1.str.contains("PARROT", flags=re.IGNORECASE, regex=True).execute() 0 False 1 False 2 True 3 False 4 False dtype: bool
使用正则表达式返回任意数字。
>>> s1.str.contains("\\d", regex=True).execute() 0 False 1 False 2 False 3 True 4 False dtype: bool
当 regex 设置为 True 时,确保 pat 不是字面模式。注意在以下示例中,可能有人会期望只有 s2[1] 和 s2[3] 返回 True。然而,'.0' 作为正则表达式会匹配任何后面跟着 0 的字符。
>>> s2 = md.Series(["40", "40.0", "41", "41.0", "35"]) >>> s2.str.contains(".0", regex=True).execute() 0 True 1 True 2 False 3 True 4 False dtype: bool