
- 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 - Timedelta
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Pandas Series.str.cat() 方法
Pandas 中的Series.str.cat()方法用於連線 Series 或 Index 中的字串,並使用給定的分隔符。此方法可以將 Series/Index 與來自另一個 Series、Index、DataFrame、NumPy 陣列或列表狀物件的元素連線起來。如果未指定其他元素,則會使用給定的分隔符將 Series/Index 中的所有值連線成單個字串。
語法
以下是 Pandas Series.str.cat() 方法的語法:
Series.str.cat(others=None, sep=None, na_rep=None, join='left')
引數
Pandas Series.str.cat() 方法接受以下引數:
others - Series、Index、DataFrame、np.ndarray 或列表狀物件,將與呼叫的 Series/Index 連線。它們必須與呼叫的 Series/Index 具有相同的長度,除了當 join 不為 None 時索引物件。
sep - 連線元素之間使用的分隔符。預設為空字串 ''。
na_rep - 缺失值的表示形式。如果為 None,則如果 others 為 None,則省略缺失值,否則在連線之前任何列中具有缺失值的行的結果將具有缺失值。
join - 指定呼叫 Series/Index 與 others 中的任何 Series/Index/DataFrame 之間的連線樣式。選項包括 {'left', 'right', 'outer', 'inner'}。預設為 'left'。
返回值
如果others 為 None,則Series.str.cat()方法返回連線的字串。否則,它返回連線物件的 Series/Index(與呼叫者相同型別)。
示例 1
這是一個使用Series.str.cat()方法將所有值連線成單個字串的基本示例。
import pandas as pd import numpy as np # Create a Series s = pd.Series(['a', 'b', np.nan, 'd']) print('Input Series:') print(s) # Concatenate without 'others' result = s.str.cat() print("Output:",result)
以上程式碼的輸出如下:
Input Series: 0 a 1 b 2 NaN 3 d dtype: object Output: abd
示例 2
此示例使用 "na_rep" 引數用給定的表示形式替換缺失值。
import pandas as pd import numpy as np # Create a Series s = pd.Series(['a', 'b', np.nan, 'd']) print('Input Series:') print(s) # Concatenate with na_rep result = s.str.cat(sep=' ', na_rep='?') print("Output:",result)
以上程式碼的輸出如下:
'a b ? d'
示例 3
此示例將輸入 Series 與 "others" 物件連線。
import pandas as pd import numpy as np # Create a Series s = pd.Series(['a', 'b', np.nan, 'd']) print('Input Series:') print(s) # Concatenate with 'others' result = s.str.cat(['A', 'B', 'C', 'D'], sep=',') print("Output:",result)
以上程式碼的輸出如下:
Input Series: 0 a 1 b 2 NaN 3 d dtype: object Output: 0 a,A 1 b,B 2 NaN 3 d,D dtype: object
示例 4
以下示例演示瞭如何使用 "join" 關鍵字連線兩個具有不同索引的 Series。
import pandas as pd import numpy as np # Create Series with different indexes s = pd.Series(['a', 'b', np.nan, 'd']) t = pd.Series(['d', 'a', 'e', 'c'], index=[3, 0, 4, 2]) # Concatenate with 'join=left' result_left = s.str.cat(t, join='left', na_rep='-') print(result_left) # Concatenate with 'join=outer' result_outer = s.str.cat(t, join='outer', na_rep='-') print(result_outer) # Concatenate with 'join=inner' result_inner = s.str.cat(t, join='inner', na_rep='-') print(result_inner) # Concatenate with 'join=right' result_right = s.str.cat(t, join='right', na_rep='-') print(result_right)
以上程式碼的輸出如下:
join='left': 0 aa 1 b- 2 -c 3 dd dtype: object join='outer': 0 aa 1 b- 2 -c 3 dd 4 -e dtype: object join='inner': 0 aa 2 -c 3 dd dtype: object join='right': 3 dd 0 aa 4 -e 2 -c dtype: object
示例 5
讓我們再看一個示例,演示Series.str.cat()方法在 Pandas DataFrame 列上的工作原理。
import pandas as pd # Read the data into a DataFrame data = {'Name': ['John', 'Jane', 'Alice'], 'Surname': ['Doe', 'Smith', 'Johnson']} df = pd.DataFrame(data) # Display the input DataFrame print("Original DataFrame:") print(df) # Join the columns df['Full Name'] = df['Name'].str.cat(df['Surname'], sep=' ') # Display the joined data print('Output Modified DataFrame:') print(df)
當我們執行以上程式時,它會產生以下結果:
Original DataFrame: Name Surname 0 John Doe 1 Jane Smith 2 Alice Johnson Output Modified DataFrame: Name Surname Full Name 0 John Doe John Doe 1 Jane Smith Jane Smith 2 Alice Johnson Alice Johnson