pandas.CategoricalIndex#

class pandas.CategoricalIndex(data=None, categories=None, ordered=None, dtype=None, copy=False, name=None)[源代码]#

基于基础数据的索引 Categorical

与CategoricalIndex一样,CategoricalIndex只能采用有限且通常是固定数量的可能值 (categories )。而且,像分类一样,它可能有一个顺序,但数字运算(加法、除法等)是不可能的。

参数
data类数组(一维)

直接词的价值。如果 categories ,则值不在 categories 将被NaN取代。

categories类似索引,可选

定义词的类别。项目必须是唯一的。如果此处未给出类别(也未在 dtype ),它们将从 data

ordered布尔值,可选

该定语是否被视为有序的定语。如果不是在这里或在 dtype ,则得到的分类将是无序的。

dtypeCategoricalDtype或“ategory”,可选

如果 CategoricalDtype ,不能与一起使用 categoriesordered

copy布尔值,默认为False

复制一份输入ndarray。

name对象,可选

要存储在索引中的名称。

加薪
ValueError

如果类别不有效。

TypeError

如果显式 ordered=True 是给的,但不是 categories 以及 values 是不可排序的。

参见

Index

基本Pandas索引类型。

Categorical

一个明确的数组。

CategoricalDtype

键入以获取分类数据。

注意事项

请参阅 user guide 想要更多。

示例

>>> pd.CategoricalIndex(["a", "b", "c", "a", "b", "c"])
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                 categories=['a', 'b', 'c'], ordered=False, dtype='category')

CategoricalIndex 也可以从 Categorical

>>> c = pd.Categorical(["a", "b", "c", "a", "b", "c"])
>>> pd.CategoricalIndex(c)
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                 categories=['a', 'b', 'c'], ordered=False, dtype='category')

有条不紊 CategoricalIndex 可以有最小值和最大值。

>>> ci = pd.CategoricalIndex(
...     ["a", "b", "c", "a", "b", "c"], ordered=True, categories=["c", "b", "a"]
... )
>>> ci
CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                 categories=['c', 'b', 'a'], ordered=True, dtype='category')
>>> ci.min()
'c'

属性

codes 

这一分类的类别代码。

categories 

这个定语的范畴。

ordered 

类别是否具有有序关系。

方法:

rename_categories \(*args, * *kwargs)

重命名类别。

reorder_categories \(*args, * *kwargs)

按照NEW_CATEGORES中指定的方式对类别进行重新排序。

add_categories \(*args, * *kwargs)

添加新类别。

remove_categories \(*args, * *kwargs)

删除指定的类别。

remove_unused_categories \(*args, * *kwargs)

删除不使用的类别。

set_categories \(*args, * *kwargs)

将类别设置为指定的NEW_CACTIONS。

as_ordered \(*args, * *kwargs)

将分类词设置为有序。

as_unordered \(*args, * *kwargs)

将定义词设置为无序。

map \(映射器)

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