当前位置: 首页 > news >正文

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

目录

一、用法精讲

801、pandas.Categorical类

801-1、语法

801-2、参数

801-3、功能

801-4、返回值

801-5、说明

801-6、用法

801-6-1、数据准备

801-6-2、代码示例

801-6-3、结果输出

802、pandas.Categorical.from_codes方法

802-1、语法

802-2、参数

802-3、功能

802-4、返回值

802-5、说明

802-6、用法

802-6-1、数据准备

802-6-2、代码示例

802-6-3、结果输出

803、pandas.Categorical.dtype属性

803-1、语法

803-2、参数

803-3、功能

803-4、返回值

803-5、说明

803-6、用法

803-6-1、数据准备

803-6-2、代码示例

803-6-3、结果输出

804、pandas.Categorical.categories属性

804-1、语法

804-2、参数

804-3、功能

804-4、返回值

804-5、说明

804-6、用法

804-6-1、数据准备

804-6-2、代码示例

804-6-3、结果输出

805、pandas.Categorical.ordered属性

805-1、语法

805-2、参数

805-3、功能

805-4、返回值

805-5、说明

805-6、用法

805-6-1、数据准备

805-6-2、代码示例

805-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

801、pandas.Categorical
801-1、语法
# 801、pandas.Categorical类
class pandas.Categorical(values, categories=None, ordered=None, dtype=None, fastpath=_NoDefault.no_default, copy=True)
Represent a categorical variable in classic R / S-plus fashion.Categoricals can only take on a limited, and usually fixed, number of possible values (categories). In contrast to statistical categorical variables, a Categorical might have an order, but numerical operations (additions, divisions, …) are not possible.All values of the Categorical are either in categories or np.nan. Assigning values outside of categories will raise a ValueError. Order is defined by the order of the categories, not lexical order of the values.Parameters:
values
list-like
The values of the categorical. If categories are given, values not in categories will be replaced with NaN.categories
Index-like (unique), optional
The unique categories for this categorical. If not given, the categories are assumed to be the unique values of values (sorted, if possible, otherwise in the order in which they appear).ordered
bool, default False
Whether or not this categorical is treated as a ordered categorical. If True, the resulting categorical will be ordered. An ordered categorical respects, when sorted, the order of its categories attribute (which in turn is the categories argument, if provided).dtype
CategoricalDtype
An instance of CategoricalDtype to use for this categorical.Raises:
ValueError
If the categories do not validate.TypeError
If an explicit ordered=True is given but no categories and the values are not sortable.
801-2、参数

801-2-1、values(必须)array-like(如列表、numpy数组等),表示输入的值,通常是一个列表、数组或其他可迭代对象,其中的数据将被转换为分类类型。

801-2-2、categories(可选,默认值为None)array-like,定义分类的类别,可以是一个列表或数组,包含所有可能的类别。如果不指定,类别将从values中提取。如果categories的元素不包含在values中,仍可将其指定,但这些类别将被视为未出现类别。

801-2-3、ordered(可选,默认值为None)布尔值,指定类别是否有顺序,如果设为True,则可用来执行有序比较。

801-2-4、dtype(可选,默认值为None)CategoricalDtype或其他类型,指定类别的数据类型,可以通过设置此参数来自定义数据类型。

801-2-5、fastpath(可选)布尔值,如果设为True,将绕过一些验证和处理步骤,适用于已经知道数据是分类的情况。

801-2-6、copy(可选,默认值为True)布尔值,指定是否复制输入的数据,如果设为True,将创建values的副本。

801-3、功能
  • 内存效率: 使用分类数据可以显著降低内存消耗,特别是在处理重复类别的情况下。
  • 提高性能: 在数据分析中,分类数据的比较和处理速度通常比非分类数据快。
  • 易于操作: 提供诸如排序、分组等功能,可以方便地进行数据操作。
801-4、返回值

        返回一个Categorical对象,其中包含:

  • 分类值: 将values变量转换为指定的分类类型。
  • 类别信息: 所有定义的类别,包括用户提供的类别和从values中提取的类别。
  • 有序性信息: 指示类别是否有序的布尔值。
801-5、说明

        无

801-6、用法
801-6-1、数据准备
801-6-2、代码示例
# 801、pandas.Categorical类
import pandas as pd
# 创建一个分类类型数据
data = ['apple', 'banana', 'orange', 'banana', 'apple']
cat_data = pd.Categorical(data, categories=['apple', 'banana', 'orange'], ordered=True)
print(cat_data)
801-6-3、结果输出
# 801、pandas.Categorical类
# ['apple', 'banana', 'orange', 'banana', 'apple']
# Categories (3, object): ['apple' < 'banana' < 'orange']
802、pandas.Categorical.from_codes方法
802-1、语法
# 802、pandas.Categorical.from_codes方法
classmethod Categorical.from_codes(codes, categories=None, ordered=None, dtype=None, validate=True)
Make a Categorical type from codes and categories or dtype.This constructor is useful if you already have codes and categories/dtype and so do not need the (computation intensive) factorization step, which is usually done on the constructor.If your data does not follow this convention, please use the normal constructor.Parameters:
codesarray-like of int
An integer array, where each integer points to a category in categories or dtype.categories, or else is -1 for NaN.categoriesindex-like, optional
The categories for the categorical. Items need to be unique. If the categories are not given here, then they must be provided in dtype.orderedbool, optional
Whether or not this categorical is treated as an ordered categorical. If not given here or in dtype, the resulting categorical will be unordered.dtypeCategoricalDtype or “category”, optional
If CategoricalDtype, cannot be used together with categories or ordered.validatebool, default True
If True, validate that the codes are valid for the dtype. If False, don’t validate that the codes are valid. Be careful about skipping validation, as invalid codes can lead to severe problems, such as segfaults.New in version 2.1.0.Returns:
Categorical
802-2、参数

802-2-1、codes(必须)整数数组,表示每个数据点对应的类别编码。

802-2-2、categories(可选,默认值为None)指定类别的顺序,如果未提供,则从codes中推断。

802-2-3、ordered(可选,默认值为None)布尔值,指定分类是否有序。

802-2-4、dtype(可选,默认值为None)指定返回的Categorical对象的dtype。

802-2-5、validate(可选,默认值为True)布尔值,如果为True,会检查codes是否有效(即是否在[0, len(categories))范围内)。

802-3、功能

        用于从整数编码和类别创建Categorical对象,它提供了一种直接从编码创建分类数据的方式,而不是从原始数据转换。

802-4、返回值

        返回一个新的Categorical对象。

802-5、说明

        无

802-6、用法
802-6-1、数据准备
802-6-2、代码示例
# 802、pandas.Categorical.from_codes方法
import pandas as pd
codes = [0, 1, 2, 1, 0]
categories = ['apple', 'banana', 'orange']
cat = pd.Categorical.from_codes(codes, categories)
print(cat)
802-6-3、结果输出
# 802、pandas.Categorical.from_codes方法
# ['apple', 'banana', 'orange', 'banana', 'apple']
# Categories (3, object): ['apple', 'banana', 'orange']
803、pandas.Categorical.dtype属性
803-1、语法
# 803、pandas.Categorical.dtype属性
property Categorical.dtype
The CategoricalDtype for this instance.
803-2、参数

        无

803-3、功能

        用于获取或设置分类数据的类型信息。

803-4、返回值

        返回一个CategoricalDtype对象,表示该分类数据的类别及其是否有序的信息。

803-5、说明

        无

803-6、用法
803-6-1、数据准备
803-6-2、代码示例
# 803、pandas.Categorical.dtype属性
import pandas as pd
# 创建一个Categorical对象
cat = pd.Categorical(['apple', 'banana', 'apple'], categories=['apple', 'banana', 'orange'], ordered=True)
# 获取dtype属性
dtype_info = cat.dtype
print(dtype_info)
# 获取类别
print(dtype_info.categories)
# 检查是否有序
print(dtype_info.ordered)
803-6-3、结果输出
# 803、pandas.Categorical.dtype属性
# category
# Index(['apple', 'banana', 'orange'], dtype='object')
# True
804、pandas.Categorical.categories属性
804-1、语法
# 804、pandas.Categorical.categories属性
property Categorical.categories
The categories of this categorical.Setting assigns new values to each category (effectively a rename of each individual category).The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories.Raises:
ValueError
If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories.
804-2、参数

        无

804-3、功能

        提供一个包含分类数据的所有唯一类别的索引对象,该索引对象可以用来查看和操作分类数据中的类别。

804-4、返回值

        返回一个Index对象,其中包含分类数据中所有唯一的类别。

804-5、说明

        无

804-6、用法
804-6-1、数据准备
804-6-2、代码示例
# 804、pandas.Categorical.categories属性
import pandas as pd
# 创建一个分类数据
data = pd.Categorical(["a", "b", "c", "a", "b"])
# 获取分类的所有唯一类别
categories = data.categories
print(categories)
804-6-3、结果输出
# 804、pandas.Categorical.categories属性
# Index(['a', 'b', 'c'], dtype='object')
805、pandas.Categorical.ordered属性
805-1、语法
# 805、pandas.Categorical.ordered属性
property Categorical.ordered
Whether the categories have an ordered relationship.
805-2、参数

        无

805-3、功能

        用来指示是否将类别视为有序(即有顺序的分类)或无序(即没有顺序的分类)。

  • 如果ordered=True,则表示分类具有一定的顺序,例如“低”、“中”、“高”。
  • 如果ordered=False,则表示分类之间没有顺序关系,例如“红色”、“蓝色”、“绿色”。
805-4、返回值

        创建Categorical对象时,如果将ordered设置为True,则返回的对象将允许进行一些顺序相关的操作,比如比较、排序等,这种情况下,可以使用Categorical的内置方法进行排序和比较。

805-5、说明

        无

805-6、用法
805-6-1、数据准备
805-6-2、代码示例
# 805、pandas.Categorical.ordered属性
import pandas as pd
# 创建一个无序的分类数据
cat_unordered = pd.Categorical(['apple', 'banana', 'orange'], ordered=False)
print(cat_unordered)
# 创建一个有序的分类数据
cat_ordered = pd.Categorical(['low', 'medium', 'high'], ordered=True)
print(cat_ordered)
# 比较顺序
print(cat_ordered[1] > cat_ordered[0])
print(cat_ordered[0] < cat_ordered[2]) 
805-6-3、结果输出
# 805、pandas.Categorical.ordered属性
# ['apple', 'banana', 'orange']
# Categories (3, object): ['apple', 'banana', 'orange']
# ['low', 'medium', 'high']
# Categories (3, object): ['high' < 'low' < 'medium']
# True
# False

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页

相关文章:

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

目录 一、用法精讲 801、pandas.Categorical类 801-1、语法 801-2、参数 801-3、功能 801-4、返回值 801-5、说明 801-6、用法 801-6-1、数据准备 801-6-2、代码示例 801-6-3、结果输出 802、pandas.Categorical.from_codes方法 802-1、语法 802-2、参数 802-3、…...

【Linux网络】基于TCP的全连接队列与文件、套接字、内核之间的关系

W...Y的主页 &#x1f60a; 代码仓库管理&#x1f495; 前言&#xff1a;之前我们已经学习了TCP传输协议&#xff0c;而无论是TCP还是UDP都是使用socket套接字进行网络传输的&#xff0c;而TCP的socket是比UDP复杂的&#xff0c;当时我们学习TCPsocket编程时使用listen函数进行…...

IDE(集成开发环境)

IDE&#xff08;集成开发环境&#xff09;是软件开发过程中不可或缺的工具&#xff0c;它集成了代码编写功能、分析功能、编译器、调试器等开发工具&#xff0c;旨在提高开发效率。不同的IDE支持不同的语言和框架&#xff0c;下面是一些通用的IDE使用技巧和插件推荐&#xff0c…...

一键导入Excel到阿里云PolarDB-MySQL版

今天&#xff0c;我将分享如何一键导入Excel到阿里云PolarDB-MySQL版数据库。 准备数据 这里&#xff0c;我们准备了一张excel表格如下&#xff1a; 连接到阿里云PolarDB 打开的卢导表&#xff0c;点击新建连接-选择阿里云PolarDB-MySQL版。如果你还没有这个工具&#xff0c;…...

Oracle有哪些版本

目录 Oracle 1(1979年) Oracle 2(1983年) Oracle 7(1992年) Oracle 8i(1999年) Oracle 9i(2001年) Oracle 10g(2004年) Oracle 11g(2007年) Oracle 12c(2013年) Oracle 18c(2018年) Oracle 19c(2019年) Oracle 21c(2023年) Oracle 23ai(202…...

先来先服务(FCFS,First-Come, First-Served)调度算法

有利于CPU繁忙作业的原因 充分利用CPU资源&#xff1a; 当一个CPU繁忙型的作业到达后&#xff0c;它会立即被执行&#xff0c;并且在没有其他作业等待的情况下&#xff0c;可以一直占用CPU直到完成。这使得CPU能够持续地执行作业&#xff0c;最大化利用CPU资源。 减少上下文切换…...

Windows操作系统忘记密码怎么办 这个方法屡试不爽 还不来试一下

Windows操作系统重置密码的操作步骤如下&#xff1a; 本方法适用于Windows Server 2008R2及其之后的操作系统。 第一步&#xff1a;从Windows 2008R2之后的操作系统光盘启动到安装界面&#xff0c;一直下一步到磁盘分区界面&#xff0c;按shiftF10调出cmd命令行界面。 第二步&…...

基于java的山区环境监督管理系统(源码+定制+开发)环境数据可视化、环境数据监测、 环境保护管理 、污染防治监测系统 大数据分析

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…...

jQuery Mobile 表单输入

jQuery Mobile 表单输入 引言 在移动设备上,表单输入是用户与移动应用交互的重要方式。jQuery Mobile 是一个基于 jQuery 的移动设备友好的开发框架,它提供了丰富的组件和工具来帮助开发者创建响应式和交互式的移动界面。本文将详细介绍如何使用 jQuery Mobile 来创建和定制…...

IoC详解

共有两类注解类型可以实现&#xff1a; 1. 类注解&#xff1a;Controller、Service、Repository、Component、Configuration. 2. 方法注解&#xff1a;Bean. 类注解 Controller&#xff08;控制器存储&#xff09; 使⽤Controller存储bean的代码如下所⽰&#xff1a; Con…...

基于 ThinkPHP+Mysql 灵活用工_灵活用工系统_灵活用工平台

基于 ThinkPHPMysql 灵活用工灵活用工平台灵活用工系统灵活用工小程序灵活用工源码灵活用工系统源码 开发语言 ThinkPHPMysql 源码合作 提供完整源代码 软件界面展示 一、企业管理后台 二、运用管理平台 三、手机端...

etcd之etcd分布式锁及事务(四)

1、etcd分布式锁及事务 1.1 前言 分布式锁是控制分布式系统之间同步访问共享资源的一种方式。在分布式系统中&#xff0c;常常需要协调他们的动作。如 果不同的系统或是同一个系统的不同主机之间共享了一个或一组资源&#xff0c;那么访问这些资源的时候&#xff0c;往往需要…...

智慧旅游微信小程序平台

作者介绍&#xff1a;✌️大厂全栈码农|毕设实战开发&#xff0c;专注于大学生项目实战开发、讲解和毕业答疑辅导。 &#x1f345;获取源码联系方式请查看文末&#x1f345; 推荐订阅精彩专栏 &#x1f447;&#x1f3fb; 避免错过下次更新 Springboot项目精选实战案例 更多项目…...

C++设计模式创建型模式———简单工厂模式、工厂方法模式、抽象工厂模式

文章目录 一、引言二、简单工厂模式三、工厂方法模式三、抽象工厂模式四、总结 一、引言 创建一个类对象的传统方式是使用关键字new &#xff0c; 因为用 new 创建的类对象是一个堆对象&#xff0c;可以实现多态。工厂模式通过把创建对象的代码包装起来&#xff0c;实现创建对…...

C++ 类与对象(中) 默认成员函数

我们知道在类中&#xff0c;有成员变量和成员函数&#xff0c;我们可以通过创造不同的成员函数来实现这个类不同的功能&#xff0c;如果我们创造一个类&#xff0c;却不实现它的成员函数会如何呢&#xff1f;这个就涉及到类中的默认成员函数的概念了。但在本文我们主要介绍以下…...

中间人攻击(https降级攻击)和iptables命令分析

中间人攻击 以下是一个简单的中间人攻击示例&#xff0c;结合 ARP 欺骗和流量修改&#xff1a; 1. 进行 ARP 欺骗 首先&#xff0c;使用 arpspoof 进行 ARP 欺骗&#xff0c;将受害者的流量重定向到攻击者的机器上&#xff1a; sudo arpspoof -i eth0 -t 172.29.144.50 172…...

开源生活-分布式管理

开源竞争&#xff08;当自己没有办法彻底掌握一门技术的时候就彻底开源掉&#xff1b;培养出更多的依赖&#xff0c;让更多人帮助你完善你的技术&#xff0c;那么这不就是在砸罐子吗&#xff1f;一个行业里面总会有人先砸罐子的&#xff0c;你不如先砸罐子&#xff0c;还能听个…...

华为OD机试真题- 关联子串

该专栏题目包含两部分&#xff1a; 100 分值部分题目 200 分值部分题目 所有题目都会陆续更新&#xff0c;订阅防丢失 题目描述&#xff1a; 给定两个字符串str1和str2&#xff0c;如果字符串str1中的字符&#xff0c;经过排列组合后的字符串中&#xff0c;只要有一个字符串是…...

云智慧完成华为原生鸿蒙系统的适配, 透视宝 APM 为用户体验保驾护航

2024 年 10 月 22 日&#xff0c;首个国产移动操作系统 —— 华为原生鸿蒙操作系统 HarmonyOS NEXT 正式面世&#xff0c;成为继 iOS 和 Android 后的全球第三大移动操作系统。HarmonyOS NEXT&#xff0c;从系统内核、数据库根基&#xff0c;到编程语言创新、AI&#xff08;人工…...

QT 多语言转换 ts、qm

QT开发之路 企业级开发系列文章&#xff0c;主要目标快速学习、完善、提升 相关技能 高效完成企业级项目开发 分享在企业中积累的实用技能和经验。 通过具体的编码过程、代码示例、步骤详解、核心内容和展示的方法解决遇到的实际问题。 阅读前声明 本系列文章属于付费内容 禁止…...

贝叶斯岭回归实战:用Python搞定金融数据预测(附完整代码)

贝叶斯岭回归实战&#xff1a;用Python搞定金融数据预测&#xff08;附完整代码&#xff09; 金融市场的波动性一直是投资者和分析师关注的焦点。在瞬息万变的股票市场中&#xff0c;能够准确预测价格走势意味着巨大的商业价值。传统的时间序列分析方法如ARIMA虽然经典&#xf…...

高效转换CSDN博客为Markdown:自动化工具与批量处理技巧

1. 为什么需要将CSDN博客转为Markdown格式 作为一个写了多年技术博客的老鸟&#xff0c;我深刻理解Markdown格式对技术写作的重要性。CSDN的富文本编辑器虽然方便&#xff0c;但存在几个致命问题&#xff1a;格式锁定在平台内、排版灵活性差、迁移成本高。而Markdown作为轻量级…...

STM32F103C8T6与HC05蓝牙模块实战:手机APP控制OLED显示(附完整代码)

STM32F103C8T6与HC05蓝牙模块实战&#xff1a;手机APP控制OLED显示&#xff08;附完整代码&#xff09; 1. 项目概述与硬件准备 在物联网终端设备交互场景中&#xff0c;蓝牙通信因其低功耗、低成本的特点成为短距离无线传输的理想选择。本项目基于STM32F103C8T6微控制器与HC05…...

CSS线性渐变实战:5分钟搞定炫酷按钮背景(附完整代码)

CSS线性渐变实战&#xff1a;5分钟搞定炫酷按钮背景&#xff08;附完整代码&#xff09; 最近在重构一个企业官网时&#xff0c;产品经理突然要求把所有按钮的纯色背景换成"更有设计感"的效果。面对30多个不同尺寸的按钮&#xff0c;手动设计图片背景显然不现实。这时…...

基于人工电场搜索智能优化算法的水库发电和供水优化调度

基于人工电场搜索智能优化算法的水库发电和供水优化调度&#xff1b; 代码为MATLAB编写&#xff0c;可直接运行&#xff1b; 含有实例数据&#xff0c;点击即可运行&#xff0c;替换成自己数据点击即可出结果&#xff0c;如图。在水库管理中&#xff0c;实现发电和供水的优化调…...

LyricsX:重构Mac音乐体验的智能歌词助手

LyricsX&#xff1a;重构Mac音乐体验的智能歌词助手 【免费下载链接】Lyrics Swift-based iTunes plug-in to display lyrics on the desktop. 项目地址: https://gitcode.com/gh_mirrors/lyr/Lyrics 当你在Mac上沉浸于音乐世界时&#xff0c;是否曾因无法同步显示歌词而…...

如何用机器学习评估专利价值?专利权利要求广度分析实战指南

如何用机器学习评估专利价值&#xff1f;专利权利要求广度分析实战指南 【免费下载链接】patents-public-data Patent analysis using the Google Patents Public Datasets on BigQuery 项目地址: https://gitcode.com/gh_mirrors/pa/patents-public-data 在知识产权竞争…...

智鼎在线测评通关秘籍:2024最新51job题库实战解析与避坑指南

智鼎在线测评通关秘籍&#xff1a;2024最新51job题库实战解析与避坑指南 在竞争激烈的求职市场中&#xff0c;智鼎在线测评已成为众多知名企业筛选人才的第一道门槛。据统计&#xff0c;2024年使用智鼎测评系统的企业数量同比增长35%&#xff0c;而通过率却不足40%。这份指南将…...

如何用League-Toolkit实现英雄联盟游戏自动化:3个核心模块深度解析

如何用League-Toolkit实现英雄联盟游戏自动化&#xff1a;3个核心模块深度解析 【免费下载链接】League-Toolkit 兴趣使然的、简单易用的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/League-Toolkit Le…...

Windows系统下安装与配置FreeSWITCH完整指南

本文提供在 Windows 系统上安装 FreeSWITCH 的完整步骤&#xff0c;涵盖下载、安装、配置、启动测试&#xff0c;以及可能遇到问题的解决方案&#xff0c;帮助你顺利完成开发环境的搭建。 一、环境准备与下载 1.1 系统要求 项目要求操作系统Windows 7/8/10/11&#xff0c;Wi…...