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

目录

一、用法精讲

361、pandas.Series.cat.codes属性

361-1、语法

361-2、参数

361-3、功能

361-4、返回值

361-5、说明

361-6、用法

361-6-1、数据准备

361-6-2、代码示例

361-6-3、结果输出

362、pandas.Series.cat.rename_categories方法

362-1、语法

362-2、参数

362-3、功能

362-4、返回值

362-5、说明

362-6、用法

362-6-1、数据准备

362-6-2、代码示例

362-6-3、结果输出

363、pandas.Series.cat.reorder_categories方法

363-1、语法

363-2、参数

363-3、功能

363-4、返回值

363-5、说明

363-6、用法

363-6-1、数据准备

363-6-2、代码示例

363-6-3、结果输出

364、pandas.Series.cat.add_categories方法

364-1、语法

364-2、参数

364-3、功能

364-4、返回值

364-5、说明

364-6、用法

364-6-1、数据准备

364-6-2、代码示例

364-6-3、结果输出

365、pandas.Series.cat.remove_categories方法

365-1、语法

365-2、参数

365-3、功能

365-4、返回值

365-5、说明

365-6、用法

365-6-1、数据准备

365-6-2、代码示例

365-6-3、结果输出

 二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

361、pandas.Series.cat.codes属性
361-1、语法
# 361、pandas.Series.cat.codes属性
pandas.Series.cat.codes
Return Series of codes as well as the index.
361-2、参数

        无

361-3、功能

        将分类数据中的每个类别映射为一个唯一的整数代码,并返回一个与原始分类数据相对应的整数序列。

361-4、返回值

        返回值是一个与原分类数据长度相同的pandas.Series对象,该Series对象的值是整数代码,代表原始分类数据中的类别。

361-5、说明

        无

361-6、用法
361-6-1、数据准备
361-6-2、代码示例
# 361、pandas.Series.cat.codes属性
import pandas as pd
# 创建一个分类序列
s = pd.Series(["dog", "cat", "dog", "fish", "cat"], dtype="category")
# 获取类别代码
codes = s.cat.codes
print(codes)
361-6-3、结果输出
# 361、pandas.Series.cat.codes属性
# 0    1
# 1    0
# 2    1
# 3    2
# 4    0
# dtype: int8
362、pandas.Series.cat.rename_categories方法
362-1、语法
# 362、pandas.Series.cat.rename_categories方法
pandas.Series.cat.rename_categories(*args, **kwargs)
Rename categories.

Parameters:
new_categories
list-like, dict-like or callable
New categories which will replace old categories.

list-like: all items must be unique and the number of items in the new categories must match the existing number of categories.

dict-like: specifies a mapping from old categories to new. Categories not contained in the mapping are passed through and extra categories in the mapping are ignored.

callable : a callable that is called on all items in the old categories and whose return values comprise the new categories.

Returns:
Categorical
Categorical with renamed categories.

Raises:
ValueError
If new categories are list-like and do not have the same number of items than the current categories or do not validate as categories.
362-2、参数

362-2-1、new_categories(必须)数组、序列或映射,指定要替换现有类别的新类别,长度必须与当前分类类别的数量相同。

362-2-2、inplace(可选,默认值为False)布尔值,如果为True,则直接修改原分类对象,而不返回新的对象;如果为False,则返回修改后的新对象,原对象保持不变。

362-2-3、ordered(可选,默认值为None)布尔值或None,如果为True,则新的分类是有序的;如果为False,则新的分类是无序的;如果设置为None,则保持原分类的顺序属性不变。

362-2-4、rename_axis(可选,默认值为None)布尔值或None,是否同时重命名轴名称。

362-3、功能

        重命名分类数据中的类别,而不会影响数据中类别的顺序或数据本身,该方法常用于需要对类别进行更有意义或更符合业务需求的名称修改时。

362-4、返回值

        如果inplace=False(默认值),返回的是一个新的pandas.Series对象,其分类类别根据new_categories被重命名;如果inplace=True,则返回None,修改直接应用于原始对象。

362-5、说明

        使用场景:

362-5-1、提高可读性和可理解性:在数据分析和处理过程中,分类数据的标签有时可能是简写、缩写或编码格式。为了提高数据的可读性,便于分析和报告,可以使用该方法将这些标签重命名为更具描述性和可理解性的名称。

362-5-2、业务需求的调整:随着业务需求的变化,数据分类标签可能需要调整以反映新的业务逻辑或结构。例如,产品类别的名称可能发生变化,需要将旧的类别名称更新为新的类别名称。

362-5-3、统一分类标签:在合并不同数据源或数据集时,不同数据源可能对同一类别使用不同的标签,使用该方法可以统一这些标签,确保数据的一致性。

362-5-4、去除不必要的分类信息:在某些情况下,原始数据的分类标签可能包含冗余或不必要的信息,使用该方法可以简化这些标签,使数据更加简洁和易于使用。

362-5-5、处理数据中的错别字或格式问题:有时候,数据分类标签可能包含拼写错误或格式不一致的问题,通过该方法可以轻松地修复这些问题。

362-5-6、数据标准化:在进行数据分析和机器学习建模时,标准化的分类标签能够帮助模型更好地理解数据,通过该方法可以确保分类标签的标准化。

362-6、用法
362-6-1、数据准备
362-6-2、代码示例
# 362、pandas.Series.cat.rename_categories方法
# 362-1、提高可读性和可理解性
import pandas as pd
# 创建分类数据
data = pd.Series(['M', 'F', 'M', 'F', 'F'], dtype='category')
# 重命名分类标签
data = data.cat.rename_categories({'M': 'Male', 'F': 'Female'})
print(data, end='\n\n')

# 362-2、业务需求的调整
import pandas as pd
# 创建分类数据
data = pd.Series(['A', 'B', 'A', 'C'], dtype='category')
# 重命名分类标签
data = data.cat.rename_categories({'A': 'Product_A', 'B': 'Product_B', 'C': 'Product_C'})
print(data, end='\n\n')

# 362-3、去除不必要的分类信息
import pandas as pd
# 创建分类数据
data = pd.Series(['Category_A', 'Category_B', 'Category_C'], dtype='category')
# 简化分类标签
data = data.cat.rename_categories({'Category_A': 'A', 'Category_B': 'B', 'Category_C': 'C'})
print(data, end='\n\n')

# 362-4、处理数据中的错别字或格式问题
import pandas as pd
# 创建包含拼写错误的分类数据
data = pd.Series(['appl', 'bananna', 'orrange'], dtype='category')
# 修正分类标签
data = data.cat.rename_categories({'appl': 'apple', 'bananna': 'banana', 'orrange': 'orange'})
print(data, end='\n\n')

# 362-5、数据标准化
import pandas as pd
# 创建分类数据
data = pd.Series(['Low', 'Medium', 'High'], dtype='category')
# 标准化分类标签
data = data.cat.rename_categories({'Low': '1', 'Medium': '2', 'High': '3'})
print(data)
362-6-3、结果输出
# 362、pandas.Series.cat.rename_categories方法
# 362-1、提高可读性和可理解性
# 0      Male
# 1    Female
# 2      Male
# 3    Female
# 4    Female
# dtype: category
# Categories (2, object): ['Female', 'Male']

# 362-2、业务需求的调整
# 0    Product_A
# 1    Product_B
# 2    Product_A
# 3    Product_C
# dtype: category
# Categories (3, object): ['Product_A', 'Product_B', 'Product_C']

# 362-3、去除不必要的分类信息
# 0    A
# 1    B
# 2    C
# dtype: category
# Categories (3, object): ['A', 'B', 'C']

# 362-4、处理数据中的错别字或格式问题
# 0     apple
# 1    banana
# 2    orange
# dtype: category
# Categories (3, object): ['apple', 'banana', 'orange']

# 362-5、数据标准化
# 0    1
# 1    2
# 2    3
# dtype: category
# Categories (3, object): ['3', '1', '2']
363、pandas.Series.cat.reorder_categories方法
363-1、语法
# 363、pandas.Series.cat.reorder_categories方法
pandas.Series.cat.reorder_categories(*args, **kwargs)
Reorder categories as specified in new_categories.

new_categories need to include all old categories and no new category items.

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

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

Returns:
Categorical
Categorical with reordered categories.

Raises:
ValueError
If the new categories do not contain all old category items or any new ones.
363-2、参数

363-2-1、new_categories(必须)数组、序列或映射,指定要替换现有类别的新类别,长度必须与当前分类类别的数量相同。

363-2-2、inplace(可选,默认值为False)布尔值,如果为True,则直接修改原分类对象,而不返回新的对象;如果为False,则返回修改后的新对象,原对象保持不变。

363-2-3、ordered(可选,默认值为None)布尔值或None,如果为True,则新的分类是有序的;如果为False,则新的分类是无序的;如果设置为None,则保持原分类的顺序属性不变。

363-3、功能

        用于在分类数据中重新排序类别,特别是在处理有序分类数据时,这个方法非常有用,因为它允许你定义类别的顺序,这在数据分析中可能会影响到排序、比较和分组操作。

363-4、返回值

        如果inplace=False(默认值),则返回一个新的Categorical或Series对象,类别按照指定顺序重新排列;如果inplace=True,则直接在原对象上进行修改,返回None。

363-5、说明

        无

363-6、用法
363-6-1、数据准备
363-6-2、代码示例
# 363、pandas.Series.cat.reorder_categories方法
import pandas as pd
# 创建一个有类别顺序的Series
cat_series = pd.Series(["low", "medium", "high"], dtype="category")
cat_series = cat_series.cat.reorder_categories(["high", "medium", "low"], ordered=True)
print(cat_series)
363-6-3、结果输出
# 363、pandas.Series.cat.reorder_categories方法
# 0       low
# 1    medium
# 2      high
# dtype: category
# Categories (3, object): ['high' < 'medium' < 'low']
364、pandas.Series.cat.add_categories方法
364-1、语法
# 364、pandas.Series.cat.add_categories方法
pandas.Series.cat.add_categories(*args, **kwargs)
Add new categories.

new_categories will be included at the last/highest place in the categories and will be unused directly after this call.

Parameters:
new_categories
category or list-like of category
The new categories to be included.

Returns:
Categorical
Categorical with new categories added.

Raises:
ValueError
If the new categories include old categories or do not validate as categories.
364-2、参数

364-2-1、new_categories(必须)数组、序列或映射,指定要替换现有类别的新类别,长度必须与当前分类类别的数量相同。

364-2-2、inplace(可选,默认值为False)布尔值,如果为True,则直接修改原分类对象,而不返回新的对象;如果为False,则返回修改后的新对象,原对象保持不变。

364-3、功能

        向现有的分类数据中添加新的类别,而不会对现有数据进行重新编码或更改现有数据点的分类标签,这在数据处理中可能非常有用,例如在开始时没有预见到所有可能的类别,但在数据分析过程中需要增加新的类别时。

364-4、返回值

        如果inplace=False(默认值),则返回一个新的Categorical或Series对象,类别按照指定顺序重新排列;如果inplace=True,则直接在原对象上进行修改,返回None。  

364-5、说明

        无

364-6、用法
364-6-1、数据准备
364-6-2、代码示例
# 364、pandas.Series.cat.add_categories方法
import pandas as pd
# 创建一个初始的分类Series
cat_series = pd.Series(["low", "medium", "high"], dtype="category")
# 添加一个新的类别"very high"
cat_series = cat_series.cat.add_categories(["very high"])
print(cat_series)
print(cat_series.cat.categories)
364-6-3、结果输出
# 364、pandas.Series.cat.add_categories方法
# 0       low
# 1    medium
# 2      high
# dtype: category
# Categories (4, object): ['high', 'low', 'medium', 'very high']
# Index(['high', 'low', 'medium', 'very high'], dtype='object')
365、pandas.Series.cat.remove_categories方法
365-1、语法
# 365、pandas.Series.cat.remove_categories方法
pandas.Series.cat.remove_categories(*args, **kwargs)
Remove the specified categories.

removals must be included in the old categories. Values which were in the removed categories will be set to NaN

Parameters:
removals
category or list of categories
The categories which should be removed.

Returns:
Categorical
Categorical with removed categories.

Raises:
ValueError
If the removals are not contained in the categories.
365-2、参数

365-2-1、new_categories(必须)数组、序列或映射,指定要替换现有类别的新类别,长度必须与当前分类类别的数量相同。

365-2-2、inplace(可选,默认值为False)布尔值,如果为True,则直接修改原分类对象,而不返回新的对象;如果为False,则返回修改后的新对象,原对象保持不变。

365-3、功能

        从现有的分类数据中移除指定的类别,这在数据清理或调整分类时非常有用,例如当某些类别不再被需要时,可以将它们从分类集合中移除。

365-4、返回值

        如果inplace=False(默认值),该方法返回一个新的Categorical或Series对象,去除了指定的类别;如果inplace=True,该方法直接修改原对象并返回None。

365-5、说明

        无

365-6、用法
365-6-1、数据准备
365-6-2、代码示例
# 365、pandas.Series.cat.remove_categories方法
import pandas as pd
# 创建一个初始的分类Series
cat_series = pd.Series(["low", "medium", "high", "medium"], dtype="category")
# 从类别中移除"high"
cat_series = cat_series.cat.remove_categories(["high"])
print(cat_series)
print(cat_series.cat.categories)
365-6-3、结果输出
# 365、pandas.Series.cat.remove_categories方法
# 0       low
# 1    medium
# 2       NaN
# 3    medium
# dtype: category
# Categories (2, object): ['low', 'medium']
# Index(['low', 'medium'], dtype='object')

 二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇夜光杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值