pandas.CategoricalIndex#
- class pandas.CategoricalIndex(data=None, categories=None, ordered=None, dtype=None, copy=False, name=None)[源代码][源代码]#
基于底层
Categorical
的索引。CategoricalIndex,类似于 Categorical,只能取有限且通常固定的可能值(categories)。同样,类似于 Categorical,它可能有顺序,但数值操作(加法、除法等)是不可能的。
- 参数:
- 数据类数组(一维)
分类的值。如果给出了 categories,不在 categories 中的值将被替换为 NaN。
- 类别类似索引的,可选的
分类的类别。项目需要是唯一的。如果在这里没有给出类别(并且在 dtype 中也没有),它们将从 data 中推断出来。
- 有序bool, 可选
这个分类是否被视为有序分类。如果在这里或 dtype 中没有给出,结果的分类将是无序的。
- dtypeCategoricalDtype 或 “category”,可选
如果
CategoricalDtype
,不能与 categories 或 ordered 一起使用。- 复制布尔值, 默认为 False
制作输入 ndarray 的副本。
- 名字对象,可选
要存储在索引中的名称。
属性
这个分类索引的类别代码。
这个分类的类别。
类别是否具有有序关系。
方法
rename_categories
(new_categories)重命名类别。
reorder_categories
(new_categories[, ordered])按照 new_categories 中的指定重新排序类别。
添加类别
(new_categories)添加新类别。
remove_categories
(removals)移除指定的类别。
移除未使用的分类。
set_categories
(new_categories[, ordered, rename])将类别设置为指定的新类别。
设置类别为有序。
将分类设置为无序。
map
(mapper[, na_action])使用输入映射或函数映射值。
- 引发:
- ValueError
如果类别未通过验证。
- TypeError
如果明确给出
ordered=True
但没有 categories 并且 values 不可排序。
参见
索引
基本的 pandas Index 类型。
分类
一个分类数组。
CategoricalDtype
分类数据的类型。
注释
更多信息请参见 用户指南。
例子
>>> 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'