
- 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.capitalize() 方法
Pandas 中的Series.str.capitalize() 方法用於將 Series 或 Index 中每個字串的首字母大寫。此方法是一種方便的方法,可以標準化文字資料的字母大小寫,確保每個字串都以大寫字母開頭,其餘字母小寫。此操作類似於 Python 中的字串方法str.capitalize()。
語法
以下是 Pandas Series.str.capitalize() 方法的語法:
Series.str.capitalize()
引數
Series.str.capitalize() 方法不接受任何引數。
返回值
Series.str.capitalize() 方法返回一個新的 Series,其中每個字串的首字母大寫,其他字母小寫。
示例 1
在這個示例中,我們透過將其應用於字串 Series 來演示Series.str.capitalize() 方法的基本用法。
import pandas as pd # Create a Series of strings s = pd.Series(['hi,', 'welcome to', 'tutorialspoint']) # Display the input Series print("Input Series") print(s) # Capitalize the first letter of each string print("Series after calling the Capitalize:") print(s.str.capitalize())
執行上述程式碼後,將產生以下輸出:
Input Series 0 hi, 1 welcome to 2 tutorialspoint dtype: object Series after calling the Capitalize: 0 Hi, 1 Welcome to 2 Tutorialspoint dtype: object
示例 2
此示例演示如何使用Series.str.capitalize() 方法格式化 DataFrame 中的“Day”列,將每個日期名稱轉換為正確的首字母大寫。
import pandas as pd # Create a DataFrame df = pd.DataFrame({'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['Math', 'english', 'science', 'music', 'games']}) print("Input DataFrame:") print(df) # Capitalize the first letter of each day df.Day = df.Day.str.capitalize() print("DataFrame after applying Capitalize:") print(df)
以下是上述程式碼的輸出:
Input DataFrame: Day Subject 0 mon Math 1 tue english 2 wed science 3 thu music 4 fri games DataFrame after applying Capitalize: Day Subject 0 Mon Math 1 Tue english 2 Wed science 3 Thu music 4 Fri games
示例 3
在這個示例中,我們將Series.str.capitalize() 方法應用於索引物件。這展示瞭如何使用它來格式化 DataFrame 中的索引標籤。
import pandas as pd # Create a DataFrame with an Index df = pd.DataFrame({'Value': [1, 2, 3]}, index=['first', 'second', 'third']) # Capitalize the first letter of each index label df.index = df.index.str.capitalize() print(df)
上述程式碼的輸出如下:
Value First 1 Second 2 Third 3
python_pandas_working_with_text_data.htm
廣告