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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
