pandas.Index#

class pandas.Index(data=None, dtype=None, copy=False, name=None, tupleize_cols=True)[源代码]#

用于索引和对齐的不可变序列。

存储所有 pandas 对象轴标签的基本对象。

在 2.0.0 版本发生变更: Index 可以容纳所有 numpy 数值 dtype(除了 float16)。之前只接受 int64/uint64/float64 dtypes。

Parameters:
dataarray-like (1-dimensional)
dtypestr, numpy.dtype, or ExtensionDtype, optional

Data type for the output Index. If not specified, this will be inferred from data. See the user guide for more usages.

copybool,默认 False

复制输入数据。

nameobject

要存储在索引中的名称。

tupleize_colsbool (default: True)

当为 True 时,尝试在可能的情况下创建 MultiIndex。

参见

RangeIndex

实现单调整数范围的 Index。

CategoricalIndex

Categorical s 的 Index。

MultiIndex

一个多级或分层 Index。

IntervalIndex

Interval s 的 Index。

DatetimeIndex

datetime64 数据的 Index。

TimedeltaIndex

timedelta64数据的索引。

PeriodIndex

Period数据的索引。

Notes

Index 实例**只能**包含可哈希的对象。Index 实例*不能*包含 numpy float16 dtype。

Examples

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

Attributes

T

返回转置,其定义就是自身。

array

支持此 Series 或 Index 的基础数据的 ExtensionArray。

dtype

返回底层数据的 dtype 对象。

empty

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

返回底层数据形状的元组。

size

返回底层数据的元素数量。

values

返回表示 Index 中数据的数组。

Methods

all (*args, **kwargs)

返回所有元素是否为真。

any (*args, **kwargs)

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

append (other)

将一系列 Index 选项附加在一起。

argmax ([axis, skipna])

返回 Series 中最大值的整数位置。

argmin ([axis, skipna])

返回 Series 中最小值的整数位置。

argsort (*args, **kwargs)

返回将对索引进行排序的整数索引。

asof (label)

返回索引中的标签,如果不存在,则返回前一个。

asof_locs (where, mask)

返回索引中标签的位置(索引)。

astype (dtype[, copy])

创建具有转换为 dtypes 的值的 Index。

copy ([name, deep])

复制此对象。

delete (loc)

删除指定位置(们)的新 Index。

diff ([periods])

计算 Index 对象中连续值之间的差异。

difference (other[, sort])

返回一个新的 Index,其中包含索引中不属于 other 的元素。

drop (labels[, errors])

删除指定标签列表的新 Index。

drop_duplicates (*[, keep])

返回去除了重复值的 Index。

droplevel ([level])

返回删除了所请求级别(们)的索引。

dropna ([how])

返回不包含 NA/NaN 值的 Index。

duplicated ([keep])

指示重复的索引值。

equals (other)

确定两个 Index 对象是否相等。

factorize ([sort, use_na_sentinel])

将对象编码为枚举类型或分类变量。

fillna ([value, downcast])

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

format ([name, formatter, na_rep])

渲染 Index 的字符串表示。

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

根据当前索引为新索引计算索引器和掩码。

get_indexer_for (target)

保证即使在非唯一情况下也能返回索引器。

get_indexer_non_unique (target)

根据当前索引为新索引计算索引器和掩码。

get_level_values (level)

返回所请求级别的值的 Index。

get_loc (key)

获取所请求标签的整数位置、切片或布尔掩码。

get_slice_bound (label, side)

计算与给定标签对应的切片边界。

groupby (values)

按给定的值数组对索引标签进行分组。

holds_integer ()

(已弃用)类型是否为整数类型。

identical (other)

类似于 equals,但会检查对象属性和类型是否也相等。

infer_objects ([copy])

如果我们有一个 object dtypes,尝试推断一个非 object dtypes。

insert (loc, item)

在新位置插入新项的新 Index。

intersection (other[, sort])

形成两个 Index 对象的交集。

is_ (other)

更灵活、更快的 is 检查,但它会通过视图工作。

is_boolean ()

(已弃用)检查 Index 是否仅包含布尔值。

is_categorical ()

(DEPRECATED) 检查索引是否包含类别数据。

is_floating ()

(DEPRECATED) 检查索引是否为浮点类型。

is_integer ()

(DEPRECATED) 检查索引是否仅包含整数。

is_interval ()

(DEPRECATED) 检查索引是否包含 Interval 对象。

is_numeric ()

(DEPRECATED) 检查索引是否仅包含数字数据。

is_object ()

(DEPRECATED) 检查索引是否为 object 数据类型。

isin (values[, level])

返回一个布尔数组,其中索引值在 values 中。

isna ()

检测缺失值。

isnull ()

检测缺失值。

item ()

将底层数据中的第一个元素作为 Python 标量返回。

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

计算 join_index 和 indexers 以使数据结构与新索引对齐。

map (mapper[, na_action])

使用输入映射或函数映射值。

max ([axis, skipna])

返回索引的最大值。

memory_usage ([deep])

值的内存使用情况。

min ([axis, skipna])

返回索引的最小值。

notna ()

检测存在的(非缺失)值。

notnull ()

检测存在的(非缺失)值。

nunique ([dropna])

返回对象中唯一元素的数量。

putmask (mask, value)

返回一个新索引,其中包含被掩码设置的值。

ravel ([order])

返回对自身的视图。

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

使用目标的值创建索引。

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])

计算输入标签和步长的切片索引器。

slice_locs ([start, end, step])

计算输入标签的切片位置。

sort (*args, **kwargs)

改用 sort_values。

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

返回索引的排序副本。

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

为了与 Index API 保持内部兼容性。

symmetric_difference (other[, result_name, sort])

计算两个 Index 对象的对称差。

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

返回一个新索引,其中包含由 indices 选择的值。

to_flat_index ()

恒等方法。

to_frame ([index, name])

创建一个 DataFrame,其中包含一个索引列。

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])

返回索引中的唯一值。

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

返回一个包含唯一值计数的 Series。

view ([cls])

where (cond[, other])

替换条件为False的值。