
- 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 - 討論
Python Pandas - 將 Series 轉換為其他物件
Pandas Series 是一種一維類似陣列的物件,包含任何型別的資料,例如整數、浮點數和字串。資料元素與標籤(索引)相關聯。在某些情況下,您需要將 Pandas Series 轉換為不同的格式以用於各種用例,例如建立列表、NumPy 陣列、字典,甚至將 Series 轉換為 DataFrame。
在本教程中,我們將學習 Pandas 中可用於將 Series 轉換為不同格式(例如列表、NumPy 陣列、字典、DataFrame 和字串)的各種方法。
以下是將 Series 轉換為其他格式的常用方法:
方法 | 描述 |
---|---|
to_list() | 將 Series 轉換為 Python 列表。 |
to_numpy() | 將 Series 轉換為 NumPy 陣列。 |
to_dict() | 將 Series 轉換為字典。 |
to_frame() | 將 Series 轉換為 DataFrame。 |
to_string() | 將 Series 轉換為字串表示形式以進行顯示。 |
將 Series 轉換為列表
Series.to_list() 方法將 Pandas Series 轉換為 Python 列表,其中 Series 的每個元素都成為返回列表中的一個元素。列表中每個元素的型別與 Series 中的型別相同。
示例
以下是使用 Series.to_list() 方法將 Pandas Series 轉換為 Python 列表的示例。
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a Python list result = s.to_list() print("Output:",result) print("Output Type:", type(result))
以下是上述程式碼的輸出:
Output: [1, 2, 3] Output Type: <class 'list'>
將 Series 轉換為 NumPy 陣列
Pandas Series.to_numpy() 方法可用於將 Pandas Series 轉換為 NumPy 陣列。此方法提供了其他功能,例如指定資料型別 (dtype)、處理缺失值 (na_value) 和控制結果是否應為副本或檢視。
示例
此示例使用 Series.to_numpy() 方法將 Series 轉換為 NumPy 陣列。
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a NumPy Array result = s.to_numpy() print("Output:",result) print("Output Type:", type(result))
Output: [1, 2, 3] Output Type: <class 'numpy.ndarray'>
將 Pandas Series 轉換為字典
Pandas Series.to_dict() 方法用於將 Series 轉換為 Python 字典,其中每個標籤(索引)都成為一個鍵,每個對應值都成為字典的值。
示例
以下示例使用 Series.to_dict() 方法將 Series 轉換為 Python 字典。
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Python dictionary result = s.to_dict() print("Output:",result) print("Output Type:", type(result))
Output: {'a': 1, 'b': 2, 'c': 3} Output Type: <class 'dict'>
將 Series 轉換為 DataFrame
Series.to_frame() 方法允許您將 Series 轉換為 DataFrame。每個 Series 都成為 DataFrame 中的一列。此方法提供了一個 name 引數來設定結果 DataFrame 的列名。
示例
此示例使用 Series.to_frame() 方法將 Series 轉換為具有單個列的 Pandas DataFrame。
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Pandas DataFrame result = s.to_frame(name='Numbers') print("Output:\n",result) print("Output Type:", type(result))
Output:
Numbers | |
---|---|
a | 1 |
b | 2 |
c | 3 |
將 Series 轉換為 Python 字串
要將 Pandas Series 物件轉換為 Python 字串,您可以使用 Series.to_string() 方法,該方法呈現 Series 的字串表示形式。
此方法返回一個字串,顯示 Series 的索引和值。您可以使用各種引數(如 na_rep(表示缺失值)、header、index、float_format、length 等)自定義輸出字串。
示例
此示例使用 Series.to_string() 方法將 Series 轉換為 Python 字串表示形式。
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['r1', 'r2', 'r3']) # Convert Series to string representation result = s.to_string() print("Output:",repr(result)) print("Output Type:", type(result))
Output: 'r1 1\nr2 2\nr3 3' Output Type: <class 'str'>