
- 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 - 索引物件
在 Pandas 中,索引物件在以結構化的方式組織和訪問資料方面發揮著重要作用。它們像帶標籤的陣列一樣工作,並在定義資料如何在Series和DataFrames等結構中排列和訪問方面發揮著重要作用。索引允許快速資料搜尋、高效切片並保持資料正確對齊,同時為每一行提供有意義的標籤。
索引用於標記 DataFrame 的行或 Series 中的元素。這些標籤可以是數字、字串或日期,它們幫助您識別資料。關於 Pandas 索引需要記住的一件重要的事情是,它們是不可變的,這意味著一旦建立,您就無法更改其大小。
在本教程中,我們將學習 Pandas 索引物件以及 Pandas 中各種型別的索引。
Index 類
Index 類是用於儲存 Pandas 物件中所有索引型別的基本物件。它提供了訪問和操作資料的基本功能。
索引物件的關鍵特徵
不可變:索引物件是不可變的序列,一旦建立就無法修改。
對齊:索引確保來自不同 DataFrame 或 Series 的資料可以根據索引值正確組合。
切片:索引允許根據標籤快速切片和檢索資料。
語法
以下是 Index 類的語法:
class pandas.Index(data=None, dtype=None, copy=False, name=None, tupleize_cols=True)
其中,
data:索引的資料,可以是類似陣列的結構(如列表或 NumPy 陣列)或其他索引物件。
dtype:它指定索引值的的資料型別,如果未提供,Pandas 將根據索引值確定資料型別。
copy:這是一個布林引數(True 或 False),它指定是否建立輸入資料的副本。
name:此引數為索引提供標籤。
data:這也是一個布林引數(True 或 False),當為 True 時,它會嘗試在可能的情況下建立 MultiIndex。
Pandas 中的索引型別
Pandas 提供各種型別的索引來處理不同型別的資料。例如:
讓我們討論 Pandas 中所有型別的索引。
數值索引
數值索引是 Pandas 中的基本索引型別,它包含數值。數值索引是預設索引,如果您未提供任何索引,Pandas 會自動分配它。
示例
以下示例演示了 Pandas 如何自動為 Pandas DataFrame 物件分配數值索引。
import pandas as pd # Generate some data for DataFrame data = { 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78] } # Creating the DataFrame df = pd.DataFrame(data) # Display the DataFrame print(df) print("\nDataFrame Index Object Type:",df.index.dtype)
以下是上述程式碼的輸出:
Name | Age | Gender | Rating | |
---|---|---|---|---|
0 | Steve | 32 | Male | 3.45 |
1 | Lia | 28 | Female | 4.60 |
2 | Vin | 45 | Male | 3.90 |
3 | Katie | 38 | Female | 2.78 |
分類索引
分類索引用於處理重複標籤。此索引在記憶體使用和處理大量重複元素方面效率很高。
示例
以下示例使用分類索引建立一個 Pandas DataFrame。
import pandas as pd # Creating a CategoricalIndex categories = pd.CategoricalIndex(['a','b', 'a', 'c']) df = pd.DataFrame({'Col1': [50, 70, 90, 60], 'Col2':[1, 3, 5, 8]}, index=categories) print("Input DataFrame:\n",df) print("\nDataFrame Index Object Type:",df.index.dtype)
以下是上述程式碼的輸出:
Input DataFrame:
Col1 | Col2 | |
---|---|---|
a | 50 | 1 |
b | 70 | 3 |
a | 90 | 5 |
c | 60 | 8 |
區間索引
區間索引用於表示資料中的區間(範圍)。此型別的索引將使用interval_range()方法建立。
示例
以下示例使用interval_range()方法建立具有區間索引的 DataFrame。
import pandas as pd # Creating a IntervalIndex interval_idx = pd.interval_range(start=0, end=4) # Creating a DataFrame with IntervalIndex df = pd.DataFrame({'Col1': [1, 2, 3, 4], 'Col2':[1, 3, 5, 8]}, index=interval_idx) print("Input DataFrame:\n",df) print("\nDataFrame Index Object Type:",df.index.dtype)
以下是上述程式碼的輸出:
Input DataFrame:
Col1 | Col2 | |
---|---|---|
(0, 1] | 1 | 1 |
(1, 2] | 2 | 3 |
(2, 3] | 3 | 5 |
(3, 4] | 4 | 8 |
多級索引
Pandas 多級索引用於表示 Pandas 資料結構索引中的多個級別或層,也稱為分層索引。
示例
以下示例顯示了建立簡單的多級索引 DataFrame。
import pandas as pd # Create MultiIndex arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']] multi_idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color')) # Create a DataFrame with MultiIndex df = pd.DataFrame({'Col1': [1, 2, 3, 4], 'Col2':[1, 3, 5, 8]}, index=multi_idx) print("MultiIndexed DataFrame:\n",df)
以下是上述程式碼的輸出:
MultiIndexed DataFrame:
Col1 | Col2 | ||
---|---|---|---|
1 | red | 1 | 1 |
blue | 2 | 3 | |
2 | red | 3 | 5 |
blue | 4 | 8 |
日期時間索引
Pandas 日期時間索引物件用於表示日期和時間值。換句話說,它用於時間序列資料,其中每一行都連結到一個特定的時間戳。
示例
以下示例使用日期時間索引建立一個 Pandas DataFrame。
import pandas as pd # Create DatetimeIndex datetime_idx = pd.DatetimeIndex(["2020-01-01 10:00:00", "2020-02-01 11:00:00"]) # Create a DataFrame with DatetimeIndex df = pd.DataFrame({'Col1': [1, 2], 'Col2':[1, 3]}, index=datetime_idx ) print("DatetimeIndexed DataFrame:\n",df)
以下是上述程式碼的輸出:
DatetimeIndexed DataFrame:
Col1 | Col2 | |
---|---|---|
2020-01-01 10:00:00 | 1 | 1 |
2020-02-01 11:00:00 | 2 | 3 |
時間增量索引
Pandas 時間增量索引用於表示兩個日期或時間之間的持續時間,例如事件之間的天數或小時數。
示例
此示例使用時間增量索引建立一個 Pandas DataFrame。
import pandas as pd # Create TimedeltaIndex timedelta_idx = pd.TimedeltaIndex(['0 days', '1 days', '2 days']) # Create a DataFrame with TimedeltaIndex df = pd.DataFrame({'Col1': [1, 2, 3], 'Col2':[1, 3, 3]}, index=timedelta_idx ) print("TimedeltaIndexed DataFrame:\n",df)
以下是上述程式碼的輸出:
TimedeltaIndexed DataFrame:
Col1 | Col2 | |
---|---|---|
0 days | 1 | 1 |
1 days | 2 | 3 |
2 days | 3 | 3 |
週期索引
Pandas 週期索引用於表示時間中的規則週期,例如季度、月份或年份。
示例
此示例使用週期索引物件建立一個 Pandas DataFrame。
import pandas as pd # Create PeriodIndex period_idx = pd.PeriodIndex(year=[2020, 2024], quarter=[1, 3]) # Create a DataFrame with PeriodIndex df = pd.DataFrame({'Col1': [1, 2], 'Col2':[1, 3]}, index=period_idx ) print("PeriodIndexed DataFrame:\n",df)
以下是上述程式碼的輸出:
PeriodIndexed DataFrame:
Col1 | Col2 | |
---|---|---|
2020Q1 | 1 | 1 |
2024Q3 | 2 | 3 |