pandas.Series.str.contains#
- Series.str.contains(pat, case=True, flags=0, na=_NoDefault.no_default, regex=True)[源代码]#
测试模式或正则表达式是否包含在 Series 或 Index 的字符串中。
根据 Series 或 Index 的字符串是否包含给定的模式或正则表达式,返回布尔 Series 或 Index。
- Parameters:
- patstr
字符序列或正则表达式。
- casebool, default True
如果为 True,则区分大小写。
- flagsint,默认为 0(无标志)。
传递给 re 模块的标志,例如 re.IGNORECASE。
- nascalar, optional
缺失值的填充值。默认值取决于数组的 dtype。对于 object-dtype,使用
numpy.nan。对于可空的StringDtype,使用pandas.NA。对于"str"dtype,使用False。- regexbool, default True
如果为 True,则假定 pat 是正则表达式。
如果为 False,则将 pat 视为字面字符串。
- Returns:
- 布尔值 Series 或 Index。
一个布尔值的 Series 或 Index,指示给定的模式是否存在于 Series 或 Index 的每个元素的字符串中。
参见
match类似,但更严格,依赖于 re.match 而不是 re.search。
Series.str.startswith测试每个字符串元素是否以某个模式开头。
Series.str.endswith与 startswith 相同,但测试字符串的结尾。
Examples
仅使用字面模式返回布尔值的 Series。
>>> 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
仅使用字面模式返回布尔值的 Index。
>>> 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')
使用 case 指定区分大小写。
>>> s1.str.contains('oG', case=True, regex=True) 0 False 1 False 2 False 3 False 4 NaN dtype: object
将 na 指定为 False 而不是 NaN 会将 NaN 值替换为 False。如果 Series 或 Index 不包含 NaN 值,则结果 dtype 将为 bool,否则为 object dtype。
>>> s1.str.contains('og', na=False, regex=True) 0 False 1 True 2 False 3 False 4 False dtype: bool
当表达式“house”或“dog”出现在字符串中时返回。
>>> s1.str.contains('house|dog', regex=True) 0 False 1 True 2 True 3 False 4 NaN dtype: object
使用 flags 和正则表达式忽略大小写。
>>> import re >>> s1.str.contains('PARROT', flags=re.IGNORECASE, regex=True) 0 False 1 False 2 True 3 False 4 NaN dtype: object
使用正则表达式返回任何数字。
>>> s1.str.contains('\\d', regex=True) 0 False 1 False 2 False 3 True 4 NaN dtype: object
确保 pat 不是字面模式,当 regex 设置为 True 时。请注意,在下面的示例中,您可能只期望 s2[1] 和 s2[3] 返回 True。然而,’.0’ 作为正则表达式会匹配任何字符后跟一个 0。
>>> 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