
- 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 - 多級索引 (MultiIndex)
- 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.find() 方法
Pandas 中的 Series.str.find() 方法用於返回 Series 中每個字串中找到指定子字串的最低索引。如果未找到子字串,則返回 -1。
此方法等效於標準 Python str.find() 方法。它可用於在 Pandas Series 或 DataFrame 的列中的字串內查詢子字串,並透過可選的開始和結束引數提供靈活性。
語法
以下是 Pandas Series.str.find() 方法的語法:
Series.str.find(sub, start=0, end=None)
引數
Series.str.find() 方法接受以下引數:
sub - 表示要搜尋的子字串的字串。
start - 可選整數,預設為 0。它表示搜尋開始的左邊緣索引。
end - 可選整數,預設為 None。它表示執行搜尋的右邊緣索引。
返回值
Series.str.find() 方法返回一個 Series 或索引,其中包含表示找到子字串的最低索引的整數。如果未找到子字串,則對於這些元素返回 -1。
示例 1
此示例演示瞭如何使用 Series.str.find() 方法在 Series 中每個字串元素中查詢子字串的最低索引。
import pandas as pd # Create a Series of strings s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples']) # Find the index of the substring 'e' in each string result = s.str.find('e') print("Input Series:") print(s) print("\nIndexes of Substring 'e':") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 'e': 0 -1 1 -1 2 6 3 6 dtype: int64
示例 2
此示例演示瞭如何在 Series 中每個字串元素的指定範圍內查詢子字串的最低索引。
import pandas as pd # Create a Series of strings s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples']) # Find the index of the substring 't' within the range [2:10] in each string result = s.str.find('t', start=2, end=10) print("Input Series:") print(s) print("\nIndexes of Substring 't' within [2:10]:") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 't' within [2:10]: 0 2 1 3 2 2 3 -1 dtype: int64
指定的範圍 [2:10] 用於限制在每個字串元素中搜索子字串 't'。
示例 3
此示例演示了當子字串不在某些元素中時,如何在 Series 中每個字串元素中查詢子字串的最低索引。
import pandas as pd # Create a Series of strings s = pd.Series(['python', 'Tutorialspoint', 'articles', 'Examples']) # Find the index of the substring 'z' in each string result = s.str.find('z') print("Input Series:") print(s) print("\nIndexes of Substring 'z':") print(result)
當我們執行以上程式碼時,它會產生以下輸出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 'z': 0 -1 1 -1 2 -1 3 -1 dtype: int64
所有元素的 -1 值表示子字串 'z' 在任何字串元素中都不存在。
python_pandas_working_with_text_data.htm
廣告