
- 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.findall() 方法
Python Pandas 中的Series.str.findall()方法用於查詢 Series 或 Index 中每個字串中模式或正則表示式的所有出現。此方法等效於將re.findall()應用於 Series/Index 中的所有元素。
該方法返回一個 Series 或 Index 的列表,其中每個列表包含在相應字串中找到的模式或正則表示式的所有不重疊匹配。它對於查詢和提取 Pandas Series、Index 或 DataFrame 列中每個字串中指定模式或正則表示式的所有不重疊出現非常有用。
語法
以下是 Pandas Series.str.findall() 方法的語法:
Series.str.findall(pat, flags=0)
引數
Series.str.findall() 方法接受以下引數:
pat - 表示要搜尋的模式或正則表示式的字串。
flags - 可選整數,預設為 0。來自 re 模組的標誌,例如 re.IGNORECASE,用於修改模式匹配行為。
返回值
Series.str.findall() 方法返回一個包含字串列表的 Series 或 Index。每個列表包含在相應字串中找到的模式或正則表示式的所有不重疊匹配。如果未找到匹配項,則為這些元素返回空列表。
示例 1
此示例演示如何在 Series 中每個字串元素中查詢子字串“t”的所有出現。
import pandas as pd # Create a Series of strings s = pd.Series(['tutorials', 'articles', 'Examples']) # Find all occurrences of the substring 't' in each string result = s.str.findall('t') print("Input Series:") print(s) print("\nOccurrences of 't':") print(result)
執行以上程式碼後,將產生以下輸出:
Input Series: 0 tutorials 1 articles 2 Examples dtype: object Occurrences of 't': 0 [t, t] 1 [t] 2 [] dtype: object
空列表[]表示元素中沒有模式出現。
示例 2
此示例演示如何使用正則表示式查詢所有模式的出現。在這裡,我們查詢所有以“t”開頭後跟任何字元的子字串。
import pandas as pd # Create a Series of strings s = pd.Series(['tutorials', 'testing', 'test cases']) # Find all substrings starting with 't' followed by any character result = s.str.findall(r't.') print("Input Series:") print(s) print("\nOccurrences of pattern 't.':") print(result)
執行以上程式碼後,將產生以下輸出:
Input Series: 0 tutorials 1 testing 2 test cases dtype: object Occurrences of pattern 't.': 0 [tu, to] 1 [te, ti] 2 [te, t ] dtype: object
輸出顯示正則表示式模式“t.”的匹配列表,其中每個元素代表與模式匹配的子字串。
示例 3
此示例演示將Series.str.findall()方法應用於 DataFrame。我們在 DataFrame 中查詢與指定模式匹配的所有電子郵件地址。
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Email': ['user1@example.com', 'info@tutorialspoint.com', 'contact@website.org'] }) # Find all occurrences of the pattern 'tutorialspoint.com' in the 'Email' column result = df['Email'].str.findall('tutorialspoint.com') print("Input DataFrame:") print(df) print("\nOccurrences of 'tutorialspoint.com':") print(result)
執行以上程式碼後,將產生以下輸出:
Input DataFrame: Email 0 user1@example.com 1 info@tutorialspoint.com 2 contact@website.org Occurrences of 'tutorialspoint.com': 0 [] 1 [tutorialspoint.com] 2 [] Name: Email, dtype: object
輸出顯示僅在第二個電子郵件地址中找到了模式“tutorialspoint.com”。