pandas.Index#

class pandas.Index(data=None, dtype=None, copy=None, name=None, tupleize_cols=True)[原始碼]#

用於索引和對齊的不可變序列。

儲存所有 pandas 物件軸標籤的基本物件。

2.0.0 版本已更改: Index 現在可以包含所有 numpy 數值 dtype(float16 除外)。之前只接受 int64/uint64/float64 dtype。

引數:
data類陣列(一維)

包含索引資料的類陣列結構。這可能是一個 Python 列表、NumPy 陣列或 pandas Series。

dtype字串、numpy.dtype 或 ExtensionDtype,可選

輸出 Index 的資料型別。如果未指定,將根據 data 推斷。更多用法請參見 使用者指南

copybool,預設為 None

是否複製輸入資料,僅對陣列、Series 和 Index 輸入有效(對於其他輸入,例如列表,總之會建立一個新陣列)。對於陣列輸入,預設為 True,對於 Index/Series,預設為 False。設定為 False 時,請自行承擔風險(如果您知道輸入資料不會在其他地方被修改)。設定為 True 時,強制預先複製 Series/Index 輸入。

name物件

儲存在索引中的名稱。

tupleize_cols布林值(預設值:True)

當為 True 時,如果可能,嘗試建立一個 MultiIndex。

另請參閱

RangeIndex

實現單調整數範圍的 Index。

CategoricalIndex

Categorical 的 Index。

MultiIndex

多級或分層 Index。

IntervalIndex

Interval 的 Index。

DatetimeIndex

datetime64 資料的索引。

TimedeltaIndex

timedelta64 資料的 Index。

PeriodIndex

Period 資料的 Index。

注意

Index 例項 **只能** 包含可雜湊物件。Index 例項 **不能** 包含 numpy float16 dtype。

示例

>>> pd.Index([1, 2, 3])
Index([1, 2, 3], dtype='int64')
>>> pd.Index(list("abc"))
Index(['a', 'b', 'c'], dtype='str')
>>> pd.Index([1, 2, 3], dtype="uint8")
Index([1, 2, 3], dtype='uint8')

屬性

T

返回轉置,按定義是自身。

array

支援此 Index 的資料的 ExtensionArray。

dtype

返回底層資料的 dtype 物件。

empty

指示 Index 是否為空。

has_duplicates

檢查 Index 是否有重複值。

hasnans

如果存在任何 NaN,則返回 True。

inferred_type

返回從值推斷出的型別字串。

is_monotonic_decreasing

如果值相等或遞減,則返回布林值。

is_monotonic_increasing

如果值相等或遞增,則返回布林值。

is_unique

返回索引是否具有唯一值。

name

返回 Index 或 MultiIndex 的名稱。

names

獲取索引上的名稱。

nbytes

返回底層資料的位元組數。

ndim

底層資料的維度數,定義為 1。

nlevels

級別數。

shape

返回底層資料的 shape 元組。

size

返回底層資料的元素數量。

values

返回表示 Index 中資料的陣列。

Methods

all(*args, **kwargs)

返回所有元素是否為真。

any(*args, **kwargs)

返回是否有任何元素為真。

append(other)

將一系列 Index 選項追加在一起。

argmax([axis, skipna])

返回 Index 中最大值的整數位置。

argmin([axis, skipna])

返回 Index 中最小值的整數位置。

argsort(*args, **kwargs)

返回將對索引進行排序的整數索引。

asof(label)

返回索引中的標籤,如果不存在,則返回前一個標籤。

asof_locs(where, mask)

返回索引中標籤的位置(索引)。

astype(dtype[, copy])

建立具有轉換為指定 dtype 的值的 Index。

copy([name, deep])

複製此物件。

delete(loc)

建立新的 Index,刪除指定的 location(-s)

diff([periods])

計算 Index 物件中連續值之間的差值。

difference(other[, sort])

返回一個新的 Index,其中包含不在 other 中的 Index 元素。

drop(labels[, errors])

建立新的 Index,刪除指定的標籤列表。

drop_duplicates(*[, keep])

返回刪除重複值的 Index。

droplevel([level])

返回移除指定 level(s) 的 Index。

dropna([how])

返回不含 NA/NaN 值的 Index。

duplicated([keep])

指示重複的索引值。

equals(other)

確定兩個 Index 物件是否相等。

factorize([sort, use_na_sentinel])

將物件編碼為列舉型別或分類變數。

fillna(value)

使用指定值填充 NA/NaN 值。

get_indexer(target[, method, limit, tolerance])

給定當前索引,計算新索引的 indexer 和 mask。

get_indexer_for(target)

保證即使在非唯一時也能返回 indexer。

get_indexer_non_unique(target)

給定當前索引,計算新索引的 indexer 和 mask。

get_level_values(level)

返回請求 level 的值的 Index。

get_loc(key)

獲取請求標籤的整數位置、切片或布林掩碼。

get_slice_bound(label, side)

計算與給定標籤對應的切片邊界。

groupby(values)

按給定值的陣列對索引標籤進行分組。

identical(other)

類似於 equals,但會檢查物件屬性和型別是否也相等。

infer_objects([copy])

如果 dtype 為 object,則嘗試推斷非 object dtype。

insert(loc, item)

在指定位置插入新項,建立新的 Index。

intersection(other[, sort])

形成兩個 Index 物件的交集。

is_(other)

類似於 `is`,但更靈活、更快速,並且可以處理檢視。

isin(values[, level])

返回一個布林陣列,其中索引值在 values 中。

isna()

檢測缺失值。

isnull()

檢測缺失值。

item()

將底層資料的第一個元素作為 Python 標量返回。

join(other, *[, how, level, ...])

計算 join_index 和 indexer 以使資料結構與新索引一致。

map(mapper[, na_action])

使用輸入對映或函式對映值。

max([axis, skipna])

返回 Index 的最大值。

memory_usage([deep])

底層資料的記憶體使用情況。

min([axis, skipna])

返回 Index 的最小值。

notna()

檢測存在的(非缺失)值。

notnull()

檢測存在的(非缺失)值。

nunique([dropna])

返回物件中唯一元素的數量。

putmask(mask, value)

返回一個新 Index,其中值根據 mask 設定。

ravel([order])

返回對自身的檢視。

reindex(target[, method, level, limit, ...])

使用 target 的值建立索引。

rename(name, *[, inplace])

更改 Index 或 MultiIndex 的名稱。

repeat(repeats[, axis])

重複 Index 中的元素。

round([decimals])

將 Index 中的每個值四捨五入到指定的小數位數。

searchsorted(value[, side, sorter])

查詢元素應插入以保持順序的索引。

set_names(names, *[, level, inplace])

設定 Index 或 MultiIndex 的名稱。

shift([periods, freq])

按指定的時間頻率增量偏移索引。

slice_indexer([start, end, step])

為輸入標籤和步長計算切片 indexer。

slice_locs([start, end, step])

為輸入標籤計算切片位置。

sort_values(*[, return_indexer, ascending, ...])

返回索引的已排序副本。

sortlevel([level, ascending, ...])

為了與 Index API 的內部相容性。

symmetric_difference(other[, result_name, sort])

計算兩個 Index 物件的對稱差。

take(indices[, axis, allow_fill, fill_value])

返回一個新 Index,包含由 indices 選擇的值。

to_flat_index()

身份方法。

to_frame([index, name])

建立一個 DataFrame,其中包含一個包含 Index 的列。

to_list()

返回值的列表。

to_numpy([dtype, copy, na_value])

Series 或 Index 中值的 NumPy ndarray 表示形式。

to_series([index, name])

建立一個 Series,其中索引和值都等於索引鍵。

tolist()

返回值的列表。

transpose(*args, **kwargs)

返回轉置,按定義是自身。

union(other[, sort])

形成兩個 Index 物件的並集。

unique([level])

返回 Index 中的唯一值。

value_counts([normalize, sort, ascending, ...])

返回一個包含唯一值計數的 Series。

view([cls])

返回具有指定 dtype 的 Index 檢視或新的 Index 例項。

where(cond[, other])

替換條件為 False 的值。