
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎
- Python Pandas - 資料結構簡介
- Python Pandas - 索引物件
- Python Pandas - 面板
- 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 - 時間增量
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Python Pandas - 處理文字資料
Pandas 提供了使用.str訪問器處理文字資料的強大工具。這允許我們在 Series 和 Index 物件上應用各種字串操作,這些操作在 Pandas DataFrame 中有效地進行字串操作。
.str訪問器提供了各種字串方法,可以對字串物件執行字串轉換、連線、搜尋等操作。下面,這些方法根據其功能進行分類:
字串轉換
此類別包括以某種方式轉換字串的方法,例如更改大小寫、格式化或修改特定字元。
序號 | 方法和描述 |
---|---|
1 |
將 Series 或 Index 中每個字串的第一個字元轉換為大寫,其餘轉換為小寫。 |
2 |
以更激進的方式將每個字串轉換為小寫,適用於不區分大小寫的比較。 |
3 |
將 Series 或 Index 中每個字串的所有字元轉換為小寫。 |
4 |
Series.str.upper() 將 Series 或 Index 中每個字串的所有字元轉換為大寫。 |
5 |
Series.str.title() 將每個字串轉換為標題大小寫,其中每個單詞的第一個字元都大寫。 |
6 |
Series.str.swapcase() 交換大小寫 - 將大寫字元轉換為小寫,反之亦然。 |
7 |
Series.str.replace() 將每個字串中模式或正則表示式的出現替換為另一個字串。 |
字串修剪
此類別包括用於將字串修剪到特定字元或指定字首的方法。
序號 | 方法和描述 |
---|---|
1 |
Series.str.lstrip() 刪除每個字串開頭的字元(預設為空格)。 |
2 |
Series.str.strip() 刪除每個字串開頭和結尾的字元(預設為空格)。 |
3 |
Series.str.rstrip() 刪除每個字串結尾的字元(預設為空格)。 |
4 |
Series.str.removeprefix(prefix) 如果存在,則從 Series 或 Index 中每個字串中刪除指定的字首。 |
5 |
Series.str.removesuffix(suffix) 如果存在,則從 Series 或 Index 中每個字串中刪除指定的尾綴。 |
字串連線和聯接方法
這些方法允許您將多個字串組合成一個,或使用指定的分割符聯接字串內的元素。
序號 | 方法和描述 |
---|---|
1 |
將 Series 或 Index 中的字串與可選的分割符連線起來。 |
2 |
使用指定的分割符聯接 Series 或 Index 中每個字串包含的列表中的元素。 |
字串填充方法
此類別包括用於將字串填充到特定長度或在指定寬度內對其對齊的方法。
序號 | 方法和描述 |
---|---|
1 |
將 Series 或 Index 中每個字串在指定的寬度內居中,用字元填充。 |
2 |
Series.str.pad() 將 Series 或 Index 中每個字串填充到指定的寬度,可以選擇從左側、右側或兩側填充。 |
3 |
用指定的字元填充 Series 或 Index 中每個字串的右側,以達到指定的寬度。 |
4 |
Series.str.rjust() 用指定的字元填充 Series 或 Index 中每個字串的左側,以達到指定的寬度。 |
5 | Series.str.zfill() 用零填充 Series 或 Index 中每個字串的左側,直到達到指定的寬度。 |
字串搜尋方法
這些方法幫助您在文字中查詢子字串、計數出現次數或檢查模式。
序號 | 方法和描述 |
---|---|
1 |
檢查 Series 或 Index 中每個字串是否包含指定的模式。 |
2 |
計算 Series 或 Index 中每個字串中模式或正則表示式的出現次數。 |
3 |
查詢 Series 或 Index 中每個字串中子字串的最低索引。 |
4 |
Series.str.rfind() 查詢 Series 或 Index 中每個字串中子字串的最高索引。 |
5 |
類似於 find(),但如果未找到子字串則引發異常。 |
6 |
Series.str.rindex() 類似於 rfind(),但如果未找到子字串則引發異常。 |
7 |
Series.str.match() 僅檢查每個字串開頭的匹配項。 |
8 |
檢查整個字串的匹配項。 |
9 |
使用正則表示式提取每個字串中匹配的組。 |
10 |
使用正則表示式提取每個字串中的所有匹配項。 |
字串分割方法
分割方法根據分隔符或模式劃分字串,這對於將文字資料解析成單獨的元件很有用。
序號 | 方法和描述 |
---|---|
1 |
Series.str.split() 按指定的分隔符或正則表示式分割 Series 或 Index 中的每個字串,並返回字串列表。 |
2 |
Series.str.rsplit() 按指定的分隔符或正則表示式分割 Series 或 Index 中的每個字串,從右側開始,並返回字串列表。 |
3 |
Series.str.partition() 在每個字串中分隔符的第一次出現處分割,並返回包含三個元素的元組:分隔符之前的部分、分隔符本身以及分隔符之後的部分。 |
4 |
Series.str.rpartition() 在每個字串中分隔符的最後一次出現處分割,並返回包含三個元素的元組:分隔符之前的部分、分隔符本身以及分隔符之後的部分。 |
字串過濾方法
這些方法對於過濾掉非字母數字字元、控制字元集或清理文字資料很有用。
序號 | 方法和描述 |
---|---|
1 |
返回提供的函式計算結果為真的元素。 |
2 |
提取指定位置每個元件的元素。 |
3 |
Series.str.get_dummies() 按指定的分隔符分割 Series 中的每個字串,並返回虛擬/指示變數的 DataFrame。 |
4 |
Series.str.isalpha() 檢查每個字串是否僅由字母字元組成。 |
5 |
Series.str.isdigit() 檢查每個字串是否僅由數字組成。 |
6 |
Series.str.isnumeric()s 檢查每個字串是否僅由數字字元組成。 |
7 |
Series.str.isspace() 檢查每個字串是否僅由空格組成。 |
8 |
Series.str.isupper() 檢查每個字串中的所有字元是否都大寫。 |
9 |
Series.str.islower() 檢查每個字串中的所有字元是否都小寫。 |
10 |
Series.str.isalnum() 檢查每個字串中的所有字元是否都為字母數字(字母和數字)。 |
11 | Series.str.istitle() 檢查 Series 或 Index 中的每個字串是否為標題大小寫,其中每個單詞都以大寫字母開頭。 |
12 |
Series.str.isdecimal() 檢查每個字串中的所有字元是否都為十進位制字元。 |
13 |
計算 Series 或 Index 中每個字串的長度。 |
14 |
查詢每個字串中模式或正則表示式的所有出現。 |
其他方法
此類別包括對字串執行各種其他操作的方法,例如編碼、解碼和檢查特定字元的存在。
序號 | 方法和描述 |
---|---|
1 |
使用指定的編碼對每個字串進行編碼。 |
2 |
使用指定的編碼解碼每個字串。 |
3 |
將製表符字元('\t')擴充套件為空格。 |
4 |
將 Series 或 Index 中每個字串重複指定的次數。 |
5 |
Series.str.slice_replace() 用傳遞的替換項替換每個字串中的切片。 |
6 |
Series.str.translate() 透過轉換表對映字串中的每個字元。 |
7 |
Series.str.slice() 將 Series 或 Index 中的每個字串根據傳遞的引數進行切片。 |
8 |
Series.str.startswith() 檢查 Series 或 Index 中的每個字串是否以指定的模式開頭。 |
9 |
檢查 Series 或 Index 中的每個字串是否以指定的模式結尾。 |
10 |
Series.str.normalize() 將 Series 或 Index 中每個字串的 Unicode 表示形式規範化為指定的規範化形式。 |
11 |
Series.str.wrap() 將 Series 或 Index 中的每個字串換行到指定的行寬,根據需要換行。 |