Python酷库之旅-第三方库Pandas(202)

目录

一、用法精讲

941、pandas.CategoricalIndex.set_categories方法

941-1、语法

941-2、参数

941-3、功能

941-4、返回值

941-5、说明

941-6、用法

941-6-1、数据准备

941-6-2、代码示例

941-6-3、结果输出

942、pandas.CategoricalIndex.as_ordered方法

942-1、语法

942-2、参数

942-3、功能

942-4、返回值

942-5、说明

942-6、用法

942-6-1、数据准备

942-6-2、代码示例

942-6-3、结果输出

943、pandas.CategoricalIndex.as_unordered方法

943-1、语法

943-2、参数

943-3、功能

943-4、返回值

943-5、说明

943-6、用法

943-6-1、数据准备

943-6-2、代码示例

943-6-3、结果输出

944、pandas.CategoricalIndex.map方法

944-1、语法

944-2、参数

944-3、功能

944-4、返回值

944-5、说明

944-6、用法

944-6-1、数据准备

944-6-2、代码示例

944-6-3、结果输出

945、pandas.CategoricalIndex.equals方法

945-1、语法

945-2、参数

945-3、功能

945-4、返回值

945-5、说明

945-6、用法

945-6-1、数据准备

945-6-2、代码示例

945-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

941、pandas.CategoricalIndex.set_categories方法
941-1、语法
python 复制代码
# 941、pandas.CategoricalIndex.set_categories方法
pandas.CategoricalIndex.set_categories(*args, **kwargs)
Set the categories to the specified new categories.

new_categories can include new categories (which will result in unused categories) or remove old categories (which results in values set to NaN). If rename=True, the categories will simply be renamed (less or more items than in old categories will result in values set to NaN or in unused categories respectively).

This method can be used to perform more than one action of adding, removing, and reordering simultaneously and is therefore faster than performing the individual steps via the more specialised methods.

On the other hand this methods does not do checks (e.g., whether the old categories are included in the new categories on a reorder), which can result in surprising changes, for example when using special string dtypes, which does not considers a S1 string equal to a single char python string.

Parameters:
new_categories
Index-like
The categories in new order.

ordered
bool, default False
Whether or not the categorical is treated as a ordered categorical. If not given, do not change the ordered information.

rename
bool, default False
Whether or not the new_categories should be considered as a rename of the old categories or as reordered categories.

Returns:
Categorical with reordered categories.
Raises:
ValueError
If new_categories does not validate as categories
941-2、参数

941-2-1、*args**(可选)****:**其他位置参数,为后续扩展功能做预留。

941-2-2、**kwargs**(可选)****:**其他关键字参数,为后续扩展功能做预留。

941-3、功能

用于更新CategoricalIndex对象的类别(categories),可以通过指定新的类别来重新定义分类顺序,该方法主要用于修改分类索引中的类别,而不会改变数据的实际值。

941-4、返回值

返回一个新的CategoricalIndex对象,其中类别被设置为new_categories,原CategoricalIndex对象保持不变(即set_categories是不可变操作)。

941-5、说明

941-6、用法
941-6-1、数据准备
python 复制代码
941-6-2、代码示例
python 复制代码
# 941、pandas.CategoricalIndex.set_categories方法
import pandas as pd
# 创建一个CategoricalIndex对象
index = pd.CategoricalIndex(["low", "medium", "high", "medium"], categories=["low", "medium", "high"], ordered=True)
# 使用set_categories修改类别顺序
new_index = index.set_categories(["high", "medium", "low"])
print(new_index)
941-6-3、结果输出
python 复制代码
# 941、pandas.CategoricalIndex.set_categories方法
# CategoricalIndex(['low', 'medium', 'high', 'medium'], categories=['high', 'medium', 'low'], ordered=True, dtype='category')
942、pandas.CategoricalIndex.as_ordered方法
942-1、语法
python 复制代码
# 942、pandas.CategoricalIndex.as_ordered方法
pandas.CategoricalIndex.as_ordered(*args, **kwargs)
Set the Categorical to be ordered.

Returns:
Categorical
Ordered Categorical.
942-2、参数

942-2-1、*args**(可选)****:**其他位置参数,为后续扩展功能做预留。

942-2-2、**kwargs**(可选)****:**其他关键字参数,为后续扩展功能做预留。

942-3、功能

用于将CategoricalIndex对象的类别变为有序类别,对于有序类别,可以执行排序、比较、以及其他相关操作,该方法不会改变类别本身的内容,只会修改其顺序属性。

942-4、返回值
  • 如果inplace=False,该方法返回一个新的CategoricalIndex对象,且该对象的类别被标记为有序。
  • 如果inplace=True,则原CategoricalIndex对象会被直接修改为有序类别,且该方法没有返回值。
942-5、说明

942-6、用法
942-6-1、数据准备
python 复制代码
942-6-2、代码示例
python 复制代码
# 942、pandas.CategoricalIndex.as_ordered方法
import pandas as pd
# 创建一个无序的CategoricalIndex对象
index = pd.CategoricalIndex(["apple", "banana", "cherry", "banana"], categories=["apple", "banana", "cherry"], ordered=False)
# 使用as_ordered将其转换为有序的CategoricalIndex
ordered_index = index.as_ordered()
print(ordered_index)
942-6-3、结果输出
python 复制代码
# 942、pandas.CategoricalIndex.as_ordered方法
# CategoricalIndex(['apple', 'banana', 'cherry', 'banana'], categories=['apple', 'banana', 'cherry'], ordered=True, dtype='category')
943、pandas.CategoricalIndex.as_unordered方法
943-1、语法
python 复制代码
# 943、pandas.CategoricalIndex.as_unordered方法
pandas.CategoricalIndex.as_unordered(*args, **kwargs)
Set the Categorical to be unordered.

Returns:
Categorical
Unordered Categorical.
943-2、参数

943-2-1、*args**(可选)****:**其他位置参数,为后续扩展功能做预留。

943-2-2、**kwargs**(可选)****:**其他关键字参数,为后续扩展功能做预留。

943-3、功能

用于将分类索引转换为无序索引。

943-4、返回值

返回一个新的Index对象,该对象是原始分类索引的无序版本,新索引中的值与原始索引中的值相同,但不再具有任何分类关系或顺序。

943-5、说明

943-6、用法
943-6-1、数据准备
python 复制代码
943-6-2、代码示例
python 复制代码
# 943、pandas.CategoricalIndex.as_unordered方法
import pandas as pd
# 创建一个分类索引
cat_index = pd.CategoricalIndex(['A', 'B', 'C', 'A', 'B', 'C'], categories=['A', 'B', 'C'], ordered=True)
print(cat_index)
# 将分类索引转换为无序索引
unordered_index = cat_index.as_unordered()
print(unordered_index)
943-6-3、结果输出
python 复制代码
# 943、pandas.CategoricalIndex.as_unordered方法
# CategoricalIndex(['A', 'B', 'C', 'A', 'B', 'C'], categories=['A', 'B', 'C'], ordered=True, dtype='category')
# CategoricalIndex(['A', 'B', 'C', 'A', 'B', 'C'], categories=['A', 'B', 'C'], ordered=False, dtype='category')
944、pandas.CategoricalIndex.map方法
944-1、语法
python 复制代码
# 944、pandas.CategoricalIndex.map方法
pandas.CategoricalIndex.map(mapper, na_action=None)
Map values using input an input mapping or function.

Maps the values (their categories, not the codes) of the index to new categories. If the mapping correspondence is one-to-one the result is a CategoricalIndex which has the same order property as the original, otherwise an Index is returned.

If a dict or Series is used any unmapped category is mapped to NaN. Note that if this happens an Index will be returned.

Parameters:
mapper
function, dict, or Series
Mapping correspondence.

Returns:
pandas.CategoricalIndex or pandas.Index
Mapped index.
944-2、参数

944-2-1、mapper**(必需)****:**一个函数或字典,用于指定映射的规则,mapper会应用到每个元素上,对CategoricalIndex中的元素逐一转换。如果mapper是一个函数,那么该函数将对每个类别值进行操作;如果是字典,类别将根据字典中定义的键值对进行替换。

944-2-2、na_action**(可选,默认值为None)****:**用于指定是否跳过缺失值(NaN)的映射操作。

  • **na_action=None:**对缺失值进行映射操作(即应用mapper)。
  • **na_action='ignore':**忽略缺失值,不对其应用mapper。
944-3、功能

将给定的mapper应用于CategoricalIndex中的每个元素,从而生成一个新的Index,每个元素的值都经过了mapper的转换。

944-4、返回值

返回一个新的Index(不是CategoricalIndex),该Index包含了映射后的结果,返回的类型将是Index或其子类,具体取决于映射结果的类型。

944-5、说明

944-6、用法
944-6-1、数据准备
python 复制代码
944-6-2、代码示例
python 复制代码
# 944、pandas.CategoricalIndex.map方法
import pandas as pd
# 创建一个CategoricalIndex
cat_index = pd.CategoricalIndex(["apple", "banana", "cherry"])
# 使用map方法,传入一个函数进行映射
mapped_index = cat_index.map(lambda x: x.upper())
print(mapped_index)
944-6-3、结果输出
python 复制代码
# 944、pandas.CategoricalIndex.map方法
# CategoricalIndex(['APPLE', 'BANANA', 'CHERRY'], categories=['APPLE', 'BANANA', 'CHERRY'], ordered=False, dtype='category')
945、pandas.CategoricalIndex.equals方法
945-1、语法
python 复制代码
# 945、pandas.CategoricalIndex.equals方法
pandas.CategoricalIndex.equals(other)
Determine if two CategoricalIndex objects contain the same elements.

Returns:
bool
True if two pandas.CategoricalIndex objects have equal elements, False otherwise.
945-2、参数

945-2-1、other**(必需)****:**另一个Index或CategoricalIndex对象,与当前的CategoricalIndex对象进行比较。

945-3、功能

检查两个CategoricalIndex是否在类别、顺序和对应的值上完全相同,如果两个CategoricalIndex对象的类别顺序、分类标签的类别,以及是否有相同的缺失值等都一致,则返回True;否则返回False。

945-4、返回值

返回一个布尔值,如果两个CategoricalIndex相等,返回True;否则返回False。

945-5、说明

945-6、用法
945-6-1、数据准备
python 复制代码
945-6-2、代码示例
python 复制代码
# 945、pandas.CategoricalIndex.equals方法
import pandas as pd
# 创建三个CategoricalIndex
cat_index1 = pd.CategoricalIndex(["apple", "banana", "cherry"], categories=["apple", "banana", "cherry"], ordered=True)
cat_index2 = pd.CategoricalIndex(["apple", "banana", "cherry"], categories=["apple", "banana", "cherry"], ordered=True)
cat_index3 = pd.CategoricalIndex(["apple", "banana", "cherry"], categories=["cherry", "banana", "apple"], ordered=True)
# 比较两个相同的CategoricalIndex
print(cat_index1.equals(cat_index2))  
# 比较两个不同的CategoricalIndex
print(cat_index1.equals(cat_index3))  
945-6-3、结果输出
python 复制代码
# 945、pandas.CategoricalIndex.equals方法 
# True
# False

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
相关推荐
ShyanZh1 分钟前
【Python3基础】01-Python 环境与开发工具
python
醇氧5 分钟前
Git 合并冲突与`__pycache__` 的恩怨情仇
python·numpy·fastapi
55873 生态系统5 分钟前
技术第4篇,【架构实战】55873 双层安全风控架构:Rust 底层硬拦截 + AI 语义研判双保险设计全解
人工智能·55873全域文明生态体系·55873操作系统·全域文明生态系统·55873技术底座
天远Date Lab5 分钟前
零信任架构实战:基于天远手机在网状态V即时版构建自动化通信网关
人工智能·ai·工具分享
Lazionr8 分钟前
二叉搜索树:从树形结构到高效查找
开发语言·c++
元岳数字人小元8 分钟前
无惧网络受限:数字人私有化部署打造离线全域服务能力
运维·人工智能·开源·人机交互·交互
此生决int10 分钟前
深入理解C++系列(21)——异常
开发语言·c++
kaixin_啊啊11 分钟前
Micam 多容器太吃资源?用 Go2RTC + EasyNVR 给小米摄像头换一套轻量本地录像方案
图像处理·人工智能·摄像头
具身AGI14 分钟前
具身ICL走热,物理AI 自主学习能力 多一条轴
人工智能
雾屿_Mistisle15 分钟前
本地 AI 工作台的 Agent 化改造:从聊天转发到 22 个工具
人工智能