
- 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.center() 方法
Pandas 中的Series.str.center() 方法用於將 Series 或 Index 中的字串的左右兩側填充到指定的寬度。
此方法確保字串在新寬度內居中,並使用指定的填充字元填充其他字元。此操作類似於 Python 中的字串方法str.center()。
語法
以下是 Pandas Series.str.center() 方法的語法 -
Series.str.center(width, fillchar=' ')
引數
Series.str.center() 方法接受以下引數 -
- width:指定結果字串的最小寬度的整數。其他字元將使用fillchar填充。
- fillchar:指定填充字元的字串,預設為空格。
返回值
Series.str.center() 方法返回一個新的 Series,其中字串已居中並使用指定的填充字元填充到指定的寬度。
示例 1
在此示例中,我們演示了Series.str.center() 方法的基本用法,將其應用於字串的 Series。
import pandas as pd # Create a Series of strings s = pd.Series(['dog', 'lion', 'panda']) # Display the input Series print("Input Series") print(s) # Center the strings with a width of 8 and fill character '.' print("Series after calling center with width=8 and fillchar='.':") print(s.str.center(8, fillchar='.'))
當我們執行以上程式碼時,它會產生以下輸出 -
Input Series 0 dog 1 lion 2 panda dtype: object Series after calling center with width=8 and fillchar='.': 0 ..dog... 1 ..lion.. 2 .panda.. dtype: object
示例 2
此示例演示瞭如何使用Series.str.center() 方法格式化 DataFrame 中的“Animal”列,透過使用自定義填充字元將每個動物名稱居中到指定的寬度。
import pandas as pd # Create a DataFrame df = pd.DataFrame({'Animal': ['dog', 'lion', 'panda'], 'Legs': [4, 4, 2]}) print("Input DataFrame:") print(df) # Center the strings in the 'Animal' column with a width of 8 and fill character '-' df['Animal'] = df['Animal'].str.center(8, fillchar='-') print("DataFrame after applying center with width=8 and fillchar='-':") print(df)
以下是以上程式碼的輸出 -
Input DataFrame: Animal Legs 0 dog 4 1 lion 4 2 panda 2 DataFrame after applying center with width=8 and fillchar='-': Animal Legs 0 --dog--- 4 1 --lion-- 4 2 -panda-- 2
示例 3
在此示例中,我們將Series.str.center() 方法應用於 Index 物件。這展示瞭如何使用它透過使用指定的寬度和填充字元居中來格式化 DataFrame 中的索引標籤。
import pandas as pd # Create a DataFrame with an Index df = pd.DataFrame({'Value': [1, 2, 3]}, index=['first', 'second', 'third']) # Display the Input DataFrame print("Input DataFrame:") print(df) # Center the index labels with a width of 10 and fill character '*' df.index = df.index.str.center(10, fillchar='*') # Display the Modified DataFrame print("Modified DataFrame:") print(df)
以上程式碼的輸出如下 -
Input DataFrame: Value first 1 second 2 third 3 Modified DataFrame: Value **first*** 1 **second** 2 **third*** 3
python_pandas_working_with_text_data.htm
廣告