pandas.CategoricalIndex#
- class pandas.CategoricalIndex(data=None, categories=None, ordered=None, dtype=None, copy=False, name=None)[源码]#
基于底层
Categorical的索引。CategoricalIndex,与 Categorical 类似,只能取有限的、通常是固定的可能值(categories)。同样,与 Categorical 类似,它可能具有顺序,但不能进行数值运算(加法、除法等)。
- 参数:
- data类数组(一维)
分类数组的值。如果提供了 categories,不在 categories 中的值将被替换为 NaN。
- categories类索引,可选
分类数组的类别。项目需要是唯一的。如果此处(或 dtype 中)未给出类别,则将从 data 中推断。
- orderedbool, optional
此分类数组是否被视为有序分类数组。如果此处或 dtype 中未给出,则生成的分类数组将是无序的。
- dtypeCategoricalDtype 或 “category”,可选
如果为
CategoricalDtype,则不能与 categories 或 ordered 一起使用。- copybool,默认值 False
复制输入 ndarray。
- nameobject,可选
存储在索引中的名称。
属性
此分类索引的类别代码。
此分类数据的类别。
类别是否具有排序关系。
Methods
rename_categories(new_categories)重命名类别。
reorder_categories(new_categories[, ordered])按 new_categories 中指定的顺序重新排列类别。
add_categories(new_categories)添加新类别。
remove_categories(removals)删除指定的类别。
删除未使用的类别。
set_categories(new_categories[, ordered, rename])将类别设置为指定的新类别。
将 Categorical 设置为有序。
将 Categorical 设置为无序。
map(mapper[, na_action])使用输入映射或函数映射值。
- 引发:
- ValueError
当类别无效时。
- TypeError
当明确给出
ordered=True但未给出 categories 且 values 不可排序时。
另请参阅
索引基础 pandas Index 类型。
Categorical一个分类数组。
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'