Pandas 系列 str.contains(~) 方法检查源 Series 的每个值是否包含指定的子字符串或正则表达式模式。
参数
1. pat | string
要检查的子字符串或正则表达式。
2. case | boolean | optional
如果 True ,则检查区分大小写。默认情况下,case=True 。
3. flags | int | optional
Python 的 re 模块中提供了一条需要遵守的规则(例如 re.IGNORECASE )。默认为 flags=0 ,即不设置规则。
4. na | scalar | optional
要替换 NaN 的值。默认情况下,NaN 保持原样。
5. regex | boolean | optional
pat 是否应解析为正则表达式。默认情况下,regex=True 。
返回值
布尔值 Series,其中 True 表示包含指定子字符串或正则表达式模式的条目。
例子
子串搜索
要检查系列中包含特定子字符串的值:
s = pd.Series(["abc","abd","efg"])
s.str.contains("ab", regex=False)
0 True
1 True
2 False
dtype: bool
使用正则表达式
由于默认情况下regex=True,我们可以直接提供正则表达式:
s = pd.Series(["a2a","a5a","aaa"])
s.str.contains("\d")
0 True
1 True
2 False
dtype: bool
在这里,我们正在检查包含单个数字的值。
指定 na
我们可以通过提供 na 参数来填充 NaN 值,如下所示:
s = pd.Series(["abc",np.nan])
s.str.contains("a", na="**")
0 True
1 **
dtype: object
相关用法
- Python Pandas Series str extractall方法用法及代码示例
- Python Pandas Series str split方法用法及代码示例
- Python Pandas Series str center方法用法及代码示例
- Python Pandas Series str pad方法用法及代码示例
- Python Pandas Series str extract方法用法及代码示例
- Python Pandas Series str replace方法用法及代码示例
- Python Pandas Series str len方法用法及代码示例
- Python Pandas Series str lower方法用法及代码示例
- Python Pandas Series str strip方法用法及代码示例
- Python Pandas Series str rstrip方法用法及代码示例
- Python Pandas Series str lstrip方法用法及代码示例
- Python Pandas Series to_list方法用法及代码示例
- Python Pandas Series between方法用法及代码示例
- Python Pandas Series map方法用法及代码示例
- Python Pandas Series hasnans属性用法及代码示例
- Python Pandas Series is_monotonic属性用法及代码示例
- Python Pandas Series to_frame方法用法及代码示例
- Python Pandas Series zfill方法用法及代码示例
- Python Pandas Series argmax方法用法及代码示例
- Python Pandas Series is_monotonic_increasing属性用法及代码示例
- Python Pandas Series is_unique属性用法及代码示例
- Python Pandas Series argmin方法用法及代码示例
- Python Pandas Series value_counts方法用法及代码示例
- Python Pandas Series is_monotonic_decreasing属性用法及代码示例
- Python Pandas Series.cumsum()用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas Series string | contains method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
