Pandas2.2 Index objects
Categorical components
方法 | 描述 |
---|---|
CategoricalIndex.codes | 用于获取分类索引中每个元素对应的整数代码 |
CategoricalIndex.categories | 用于获取分类索引中的所有类别(categories) |
CategoricalIndex.ordered | 用于指示分类索引中的类别是否具有有序关系 |
CategoricalIndex.rename_categories(*args, …) | 用于重命名分类索引中的类别 |
CategoricalIndex.reorder_categories(*args, …) | 用于重新排列分类索引中的类别顺序 |
CategoricalIndex.add_categories(*args, **kwargs) | 用于向分类索引中添加新的类别 |
CategoricalIndex.remove_categories(*args, …) | 用于从分类索引中移除指定的类别 |
CategoricalIndex.remove_unused_categories(…) | 用于移除分类索引中未使用的分类(categories) |
CategoricalIndex.set_categories(*args, **kwargs) | 用于设置新的分类(categories) |
CategoricalIndex.as_ordered(*args, **kwargs) | 用于将无序的分类索引转换为有序的分类索引 |
pandas.CategoricalIndex.as_ordered()
pandas.CategoricalIndex.as_ordered()
是 [CategoricalIndex] 对象的一个方法,用于将无序的分类索引转换为有序的分类索引。
详细说明
- 用途:将无序的 [CategoricalIndex] 转换为有序的 [CategoricalIndex]
- 返回值:一个新的有序 [CategoricalIndex] 对象
示例代码及结果
示例 1: 基本用法 - 将无序分类转换为有序分类
import pandas as pd
# 创建一个无序的 CategoricalIndex
values = ['small', 'large', 'medium', 'small', 'large']
categories = ['small', 'medium', 'large']
cat_index = pd.CategoricalIndex(values, categories=categories, ordered=False)
print("原始 CategoricalIndex:")
print(cat_index)
print("是否有序:", cat_index.ordered)
# 转换为有序分类
ordered_cat_index = cat_index.as_ordered()
print("\n转换为有序分类后:")
print(ordered_cat_index)
print("是否有序:", ordered_cat_index.ordered)
输出结果:
原始 CategoricalIndex:
CategoricalIndex(['small', 'large', 'medium', 'small', 'large'],
categories=['small', 'medium', 'large'],
ordered=False, dtype='category')
是否有序: False
转换为有序分类后:
CategoricalIndex(['small', 'large', 'medium', 'small', 'large'],
categories=['small', 'medium', 'large'],
ordered=True, dtype='category')
是否有序: True
示例 2: 在 DataFrame 中使用
import pandas as pd
# 创建一个 DataFrame,使用无序的 CategoricalIndex 作为索引
data = {
'sales': [100, 200, 150, 300],
'profit': [20, 40, 30, 60]
}
values = ['North', 'South', 'East', 'West']
categories = ['North', 'South', 'East', 'West']
cat_index = pd.CategoricalIndex(values, categories=categories, ordered=False, name='region')
df = pd.DataFrame(data, index=cat_index)
print("原始 DataFrame:")
print(df)
print("索引是否有序:", df.index.ordered)
# 将索引转换为有序分类
df.index = df.index.as_ordered()
print("\n将索引转换为有序分类后:")
print(df)
print("索引是否有序:", df.index.ordered)
输出结果:
原始 DataFrame:
sales profit
region
North 100 20
South 200 40
East 150 30
West 300 60
索引是否有序: False
将索引转换为有序分类后:
sales profit
region
North 100 20
South 200 40
East 150 30
West 300 60
索引是否有序: True
示例 3: 对已有序的分类使用 as_ordered()
import pandas as pd
# 创建一个有序的 CategoricalIndex
values = ['A', 'C', 'B', 'A']
categories = ['A', 'B', 'C']
cat_index = pd.CategoricalIndex(values, categories=categories, ordered=True)
print("原始有序 CategoricalIndex:")
print(cat_index)
print("是否有序:", cat_index.ordered)
# 对已有序的分类使用 as_ordered()
new_cat_index = cat_index.as_ordered()
print("\n对已有序的分类使用 as_ordered():")
print(new_cat_index)
print("是否为同一对象:", cat_index is new_cat_index)
print("是否有序:", new_cat_index.ordered)
输出结果:
原始有序 CategoricalIndex:
CategoricalIndex(['A', 'C', 'B', 'A'],
categories=['A', 'B', 'C'],
ordered=True, dtype='category')
是否有序: True
对已有序的分类使用 as_ordered():
CategoricalIndex(['A', 'C', 'B', 'A'],
categories=['A', 'B', 'C'],
ordered=True, dtype='category')
是否为同一对象: True
是否有序: True
示例 4: 与 as_unordered() 方法的比较
import pandas as pd
# 创建一个无序的 CategoricalIndex
values = ['red', 'blue', 'green', 'red']
categories = ['red', 'blue', 'green']
cat_index = pd.CategoricalIndex(values, categories=categories, ordered=False)
print("原始无序 CategoricalIndex:")
print(cat_index)
print("是否有序:", cat_index.ordered)
# 使用 as_ordered() 转换为有序
ordered_cat_index = cat_index.as_ordered()
print("\n使用 as_ordered() 后:")
print(ordered_cat_index)
print("是否有序:", ordered_cat_index.ordered)
# 使用 as_unordered() 转换为无序
unordered_cat_index = ordered_cat_index.as_unordered()
print("\n使用 as_unordered() 后:")
print(unordered_cat_index)
print("是否有序:", unordered_cat_index.ordered)
输出结果:
原始无序 CategoricalIndex:
CategoricalIndex(['red', 'blue', 'green', 'red'],
categories=['red', 'blue', 'green'],
ordered=False, dtype='category')
是否有序: False
使用 as_ordered() 后:
CategoricalIndex(['red', 'blue', 'green', 'red'],
categories=['red', 'blue', 'green'],
ordered=True, dtype='category')
是否有序: True
使用 as_unordered() 后:
CategoricalIndex(['red', 'blue', 'green', 'red'],
categories=['red', 'blue', 'green'],
ordered=False, dtype='category')
是否有序: False
示例 5: 排序操作的差异
import pandas as pd
# 创建无序和有序的 CategoricalIndex
values = ['C', 'A', 'B', 'C']
categories = ['A', 'B', 'C']
unordered_cat = pd.CategoricalIndex(values, categories=categories, ordered=False)
ordered_cat = pd.CategoricalIndex(values, categories=categories, ordered=True)
print("无序 CategoricalIndex:")
print(unordered_cat)
print("排序结果:")
print(unordered_cat.sort_values())
print("\n有序 CategoricalIndex:")
print(ordered_cat)
print("排序结果:")
print(ordered_cat.sort_values())
输出结果:
无序 CategoricalIndex:
CategoricalIndex(['C', 'A', 'B', 'C'],
categories=['A', 'B', 'C'],
ordered=False, dtype='category')
排序结果:
CategoricalIndex(['A', 'B', 'C', 'C'],
categories=['A', 'B', 'C'],
ordered=False, dtype='category')
有序 CategoricalIndex:
CategoricalIndex(['C', 'A', 'B', 'C'],
categories=['A', 'B', 'C'],
ordered=True, dtype='category')
排序结果:
CategoricalIndex(['A', 'B', 'C', 'C'],
categories=['A', 'B', 'C'],
ordered=True, dtype='category')
应用场景
- 数据分析:在需要对分类数据进行排序或比较操作时,将无序分类转换为有序分类
- 数据可视化:在绘图时,有序分类可以按照指定顺序显示
- 统计分析:在进行统计分析时,有序分类可以提供更多信息
- 数据处理:在需要利用分类顺序进行数据处理时
- 机器学习:在某些机器学习算法中,有序分类可能有特殊处理方式
注意事项
- 该方法只适用于 [CategoricalIndex],不适用于普通的
Index
- 对于已经是有序的 [CategoricalIndex],使用该方法会返回原对象
- 转换为有序分类后,可以进行大小比较操作
- 分类的顺序按照 [categories] 参数中指定的顺序
通过 as_ordered()
方法,我们可以方便地将无序的分类索引转换为有序的分类索引,这在数据分析和处理中非常有用。