
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎
- Python Pandas - 資料結構介紹
- Python Pandas - 索引物件
- Python Pandas - 面板 (Panel)
- Python Pandas - 基本功能
- Python Pandas - 索引和資料選擇
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - 切片 Series 物件
- Python Pandas - Series 物件的屬性
- Python Pandas - Series 物件的算術運算
- Python Pandas - 將 Series 轉換為其他物件
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - 訪問 DataFrame
- Python Pandas - 切片 DataFrame 物件
- Python Pandas - 修改 DataFrame
- Python Pandas - 從 DataFrame 中刪除行
- Python Pandas - DataFrame 的算術運算
- Python Pandas - I/O 工具
- Python Pandas - I/O 工具
- Python Pandas - 處理 CSV 格式
- Python Pandas - 讀取和寫入 JSON 檔案
- Python Pandas - 從 Excel 檔案讀取資料
- Python Pandas - 將資料寫入 Excel 檔案
- Python Pandas - 處理 HTML 資料
- Python Pandas - 剪貼簿
- Python Pandas - 處理 HDF5 格式
- Python Pandas - 與 SQL 的比較
- Python Pandas - 資料處理
- Python Pandas - 排序
- Python Pandas - 重索引
- Python Pandas - 迭代
- Python Pandas - 連線
- Python Pandas - 統計函式
- Python Pandas - 描述性統計
- Python Pandas - 處理文字資料
- Python Pandas - 函式應用
- Python Pandas - 選項和自定義
- Python Pandas - 視窗函式
- Python Pandas - 聚合
- Python Pandas - 合併/連線
- Python Pandas - 多級索引
- Python Pandas - 多級索引基礎
- Python Pandas - 使用多級索引進行索引
- Python Pandas - 使用多級索引進行高階重索引
- Python Pandas - 重新命名多級索引標籤
- Python Pandas - 對多級索引進行排序
- Python Pandas - 二元運算
- Python Pandas - 二元比較運算
- Python Pandas - 布林索引
- Python Pandas - 布林掩碼
- Python Pandas - 資料重塑和透視表
- Python Pandas - 透視表
- Python Pandas - 堆疊和取消堆疊
- Python Pandas - 熔化
- Python Pandas - 計算虛擬變數
- Python Pandas - 分類資料
- Python Pandas - 分類資料
- Python Pandas - 分類資料的排序和分類
- Python Pandas - 分類資料的比較
- Python Pandas - 處理缺失資料
- Python Pandas - 缺失資料
- Python Pandas - 填充缺失資料
- Python Pandas - 缺失值的插值
- Python Pandas - 刪除缺失資料
- Python Pandas - 對缺失資料進行計算
- Python Pandas - 處理重複資料
- Python Pandas - 重複資料
- Python Pandas - 計數和檢索唯一元素
- Python Pandas - 重複標籤
- Python Pandas - 分組和聚合
- Python Pandas - GroupBy
- Python Pandas - 時間序列資料
- Python Pandas - 日期功能
- Python Pandas - 時間增量 (Timedelta)
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Pandas Series.str.fullmatch() 方法
Pandas 中的 Series.str.fullmatch() 方法用於確定 Series 中的每個字串是否完全匹配指定的正則表示式模式。
當您想要驗證整個字串是否符合給定的格式或模式時,此方法很有用。它等同於將 re.fullmatch() 應用於 Series 中的每個字串。
語法
以下是 Pandas Series.str.fullmatch() 方法的語法:
Series.str.fullmatch(pat, case=True, flags=0, na=None)
引數
Series.str.fullmatch() 方法接受以下引數:
pat - 表示要匹配的字元序列或正則表示式模式的字串。
case - 布林值,預設為 True。如果為 True,則匹配區分大小寫。
flags - 可選整數,預設為 0。來自 re 模組的標誌,例如 re.IGNORECASE,用於修改模式匹配行為。
na - 用於缺失值的可選標量值。如果未指定,則預設值取決於 Series 的資料型別。對於 object-dtype,使用 numpy.nan。對於 StringDtype,使用 pandas.NA。
返回值
Series.str.fullmatch() 方法返回一個包含布林值的 Series 或 Index。每個布林值指示 Series 中相應的字串是否完全匹配給定的正則表示式模式。
示例 1
此示例演示如何檢查 Series 中的每個字串是否完全匹配有效電子郵件地址的正則表示式模式。
import pandas as pd # Create a Series of strings s = pd.Series(['user@example.com', 'user@domain', 'example.com', 'test@tutorialspoint.com']) # Check if each string fully matches the pattern for an email address result = s.str.fullmatch(r'\w+@\w+\.\w+') print("Input Series:") print(s) print("\nFull Match Results:") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input Series: 0 user@example.com 1 user@domain 2 example.com 3 test@tutorialspoint.com dtype: object Full Match Results: 0 True 1 False 2 False 3 True dtype: bool
輸出顯示,只有完全匹配電子郵件模式的字串才會標記為 True。
示例 2
此示例演示如何使用 Series.str.fullmatch() 方法檢查每個字串是否完全匹配格式為 'YYYY-MM-DD' 的日期的模式。
import pandas as pd # Create a Series of strings s = pd.Series(['2024-07-29', '2024-07-29 00:00:00', '2024-07-29T00:00:00', '07-29-2024']) # Check if each string fully matches the date pattern result = s.str.fullmatch(r'\d{4}-\d{2}-\d{2}') print("Input Series:") print(s) print("\nFull Match Results:") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input Series: 0 2024-07-29 1 2024-07-29 00:00:00 2 2024-07-29T00:00:00 3 07-29-2024 dtype: object Full Match Results: 0 True 1 False 2 False 3 False dtype: bool
示例 3
此示例演示如何檢查 DataFrame 列中的每個字串是否完全匹配日期模式,同時處理缺失值。
import pandas as pd # Create a DataFrame with date strings df = pd.DataFrame({ 'date': ['2024-07-29', '2024-07-29 00:00:00', '2024-07-29', None] }) # Check if each string fully matches the date pattern, treating NaNs as True result = df['date'].str.fullmatch(r'\d{4}-\d{2}-\d{2}', na=True) print("Input DataFrame:") print(df) print("\nFull Match Results:") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input DataFrame: date 0 2024-07-29 1 2024-07-29 00:00:00 2 2024-07-29 3 None Full Match Results: 0 True 1 False 2 True 3 True Name: date, dtype: bool
在這種情況下,由於 na=True 引數,NaN 值被視為 True,而其他字串則根據模式進行匹配。