
- 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 - 討論
Pandas Series.str.casefold() 方法
Pandas 中的 Series.str.casefold() 方法用於將 Series 或 Index 中的字串轉換為小寫形式。Casefolding 是一種更激進的小寫轉換形式,用於文字規範化。它尤其適用於執行不區分大小寫的比較以及以更統一的方式處理文字。
此方法等效於 Python 的內建 str.casefold() 方法,通常用於資料分析任務中標準化文字資料。
語法
以下是 Pandas Series.str.casefold() 方法的語法:
Series.str.casefold()
引數
Pandas Series.str.casefold() 方法不接受任何引數。
返回值
Series.str.casefold() 方法返回一個形狀相同的 Series 或 Index,其中每個字串都已轉換為小寫形式。這意味著每個字串中的所有字元都轉換為其小寫形式。
示例 1
讓我們來看一個基本的示例,以瞭解 Series.str.casefold() 方法的工作原理:
import pandas as pd # Create a Series s = pd.Series(['Hi', 'WELCOME to', 'TUTORIALSPOINT']) # Display the input Series print("Input Series") print(s) # Apply the casefold method print("Series after applying the casefold:") print(s.str.casefold())
執行以上程式時,它會產生以下結果:
Input Series 0 Hi 1 WELCOME to 2 TUTORIALSPOINT dtype: object Series after applying the casefold: 0 hi 1 welcome to 2 tutorialspoint dtype: object
示例 2
在此示例中,我們將演示在 DataFrame 中使用 Series.str.casefold() 方法:
import pandas as pd # Create a DataFrame df = pd.DataFrame({'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Subject': ['Math', 'English', 'Science', 'Music', 'Games']}) # Print the original DataFrame print("Input DataFrame") print(df) # Apply the casefold method to the 'Day' column df.Day = df.Day.str.casefold() # Print the modified DataFrame print("Modified DataFrame:") print(df)
以上程式碼的輸出如下:
Original DataFrame: Day Subject 0 Mon Math 1 Tue English 2 Wed Science 3 Thu Music 4 Fri Games Modified DataFrame: Day Subject 0 mon Math 1 tue English 2 wed Science 3 thu Music 4 fri Games
示例 3
讓我們再看一個示例,其中我們在更復雜的場景中應用 Series.str.casefold():
import pandas as pd # Create a DataFrame with mixed-case text df = pd.DataFrame({'Name': ['Alice', 'Bob', 'CHARLIE', 'david'], 'Role': ['Admin', 'user', 'MANAGER', 'staff']}) # Print the original DataFrame print("Original DataFrame:") print(df) # Apply casefold to both 'Name' and 'Role' columns df = df.apply(lambda x: x.str.casefold() if x.dtype == "object" else x) # Print the modified DataFrame print("Modified DataFrame:") print(df)
以上程式碼的輸出如下:
Original DataFrame: Name Role 0 Alice Admin 1 Bob user 2 CHARLIE MANAGER 3 david staff Modified DataFrame: Name Role 0 alice admin 1 bob user 2 charlie manager 3 david staff
python_pandas_working_with_text_data.htm
廣告