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

news2024/9/20 5:51:53

目录

一、用法精讲

226、pandas.Series.pad方法

226-1、语法

226-2、参数

226-3、功能

226-4、返回值

226-5、说明

226-6、用法

226-6-1、数据准备

226-6-2、代码示例

226-6-3、结果输出

227、pandas.Series.replace方法

227-1、语法

227-2、参数

227-3、功能

227-4、返回值

227-5、说明

227-6、用法

227-6-1、数据准备

227-6-2、代码示例

227-6-3、结果输出

228、pandas.Series.argsort方法

228-1、语法

228-2、参数

228-3、功能

228-4、返回值

228-5、说明

228-6、用法

228-6-1、数据准备

228-6-2、代码示例

228-6-3、结果输出

229、pandas.Series.argmin方法

229-1、语法

229-2、参数

229-3、功能

229-4、返回值

229-5、说明

229-6、用法

229-6-1、数据准备

229-6-2、代码示例

229-6-3、结果输出

230、pandas.Series.argmax方法

230-1、语法

230-2、参数

230-3、功能

230-4、返回值

230-5、说明

230-6、用法

230-6-1、数据准备

230-6-2、代码示例

230-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

226、pandas.Series.pad方法
226-1、语法
# 226、pandas.Series.pad方法
pandas.Series.pad(*, axis=None, inplace=False, limit=None, downcast=_NoDefault.no_default)
Fill NA/NaN values by propagating the last valid observation to next valid.

Deprecated since version 2.0: Series/DataFrame.pad is deprecated. Use Series/DataFrame.ffill instead.

Returns:
Series/DataFrame or None
Object with missing values filled or None if inplace=True.
226-2、参数

226-2-1、axis(可选,默认值为None)用于确定操作的轴,对于Series来说,它的作用不大,因为Series是一维的,通常不需要指定轴。

226-2-2、inplace(可选,默认值为False)如果设为True,则会在原地修改Series 对象而不是返回一个新的对象。

226-2-3、limit(可选,默认值为None)一个整数,指定填充的最大范围,即如果填充的连续缺失值超过这个限制,填充将会停止。

226-2-4、downcast(可选)用于控制数据类型的转换,通常情况下,你不需要设置这个参数,它在自动调整数据类型时会用到。

226-3、功能

        用于填充Series对象中的缺失值(例如NaN),使用前一个有效值进行填充,这在处理时间序列数据时特别有用,可以保持数据的连续性。

226-4、返回值

         返回一个Series对象,其中所有的缺失值都被填充为前一个有效值,如果inplace=True,则直接在原Series对象上修改,不返回新对象

226-5、说明

        与pandas.Series.ffill方法的功能相同。

226-6、用法
226-6-1、数据准备
226-6-2、代码示例
# 226、pandas.Series.pad方法
import pandas as pd
import numpy as np
# 创建一个包含缺失值的Series
s = pd.Series([1, np.nan, 3, np.nan, 5])
# 使用pad方法填充缺失值
result = s.pad()
print(result)
226-6-3、结果输出
# 226、pandas.Series.pad方法
# 0    1.0
# 1    1.0
# 2    3.0
# 3    3.0
# 4    5.0
# dtype: float64
227、pandas.Series.replace方法
227-1、语法
# 227、pandas.Series.replace方法
pandas.Series.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)
Replace values given in to_replace with value.

Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.

Parameters:
to_replacestr, regex, list, dict, Series, int, float, or None
How to find the values that will be replaced.

numeric, str or regex:

numeric: numeric values equal to to_replace will be replaced with value

str: string exactly matching to_replace will be replaced with value

regex: regexs matching to_replace will be replaced with value

list of str, regex, or numeric:

First, if to_replace and value are both lists, they must be the same length.

Second, if regex=True then all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use.

str, regex and numeric rules apply as above.

dict:

Dicts can be used to specify different replacement values for different existing values. For example, {'a': 'b', 'y': 'z'} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way, the optional value parameter should not be given.

For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column ‘a’ and the value ‘z’ in column ‘b’ and replaces these values with whatever is specified in value. The value parameter should not be None in this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in.

For a DataFrame nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with NaN. The optional value parameter should not be specified to use a nested dict in this way. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions.

None:

This means that the regex argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. If value is also None then this must be a nested dictionary or Series.

See the examples section for examples of each of these.

valuescalar, dict, list, str, regex, default None
Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed.

inplacebool, default False
If True, performs operation inplace and returns None.

limitint, default None
Maximum size gap to forward or backward fill.

Deprecated since version 2.1.0.

regexbool or same types as to_replace, default False
Whether to interpret to_replace and/or value as regular expressions. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None.

method{‘pad’, ‘ffill’, ‘bfill’}
The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.

Deprecated since version 2.1.0.

Returns:
Series/DataFrame
Object after replacement.

Raises:
AssertionError
If regex is not a bool and to_replace is not None.

TypeError
If to_replace is not a scalar, array-like, dict, or None

If to_replace is a dict and value is not a list, dict, ndarray, or Series

If to_replace is None and regex is not compilable into a regular expression or is a list, dict, ndarray, or Series.

When replacing multiple bool or datetime64 objects and the arguments to to_replace does not match the type of the value being replaced

ValueError
If a list or an ndarray is passed to to_replace and value but they are not the same length.
227-2、参数

227-2-1、to_replace(可选,默认值为None)用于指定要被替换的值,可以是单个值、列表、字典、正则表达式等,该参数定义了要查找和替换的内容。

227-2-2、value(可选)用于指定替换to_replace中值的内容,可以是单个值、列表或字典,如果to_replace是字典,那么value必须也是字典。

227-2-3、inplace(可选,默认值为False)如果设为True,则直接在原Series对象上进行替换,而不是返回一个新的Series对象。

227-2-4、limit(可选,默认值为None)一个整数,指定最大替换次数,即如果替换的次数超过这个限制,替换将会停止。

227-2-5、regex(可选,默认值为False)如果设为True,to_replace被解释为正则表达式,并进行模式匹配替换。

227-2-6、method(可选)用于指定填充方法,如果使用了填充方法(如'ffill'或'bfill'),value和to_replace参数将被忽略。

227-3、功能

        用于将Series中的特定值替换为其他指定的值,这对于数据清洗和预处理特别有用。        

227-4、返回值

        返回一个Series对象,其中指定的值已经被替换,如果inplace=True,则直接在原Series对象上修改,不返回新对象。

227-5、说明

        无

227-6、用法
227-6-1、数据准备
227-6-2、代码示例
# 227、pandas.Series.replace方法
import pandas as pd
# 创建一个包含不同值的Series
s = pd.Series(['apple', 'banana', 'cherry', 'banana', 'apple'])
# 使用replace方法替换'banana'为'orange'
result = s.replace(to_replace='banana', value='orange')
print(result)
227-6-3、结果输出
# 227、pandas.Series.replace方法
# 0     apple
# 1    orange
# 2    cherry
# 3    orange
# 4     apple
# dtype: object
228、pandas.Series.argsort方法
228-1、语法
# 228、pandas.Series.argsort方法
pandas.Series.argsort(axis=0, kind='quicksort', order=None, stable=None)
Return the integer indices that would sort the Series values.

Override ndarray.argsort. Argsorts the value, omitting NA/null values, and places the result in the same locations as the non-NA values.

Parameters:
axis
{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.

kind
{‘mergesort’, ‘quicksort’, ‘heapsort’, ‘stable’}, default ‘quicksort’
Choice of sorting algorithm. See numpy.sort() for more information. ‘mergesort’ and ‘stable’ are the only stable algorithms.

order
None
Has no effect but is accepted for compatibility with numpy.

stable
None
Has no effect but is accepted for compatibility with numpy.

Returns:
Series[np.intp]
Positions of values within the sort order with -1 indicating nan values.
228-2、参数

228-2-1、axis(可选,默认值为0)整数,指定轴进行排序,对于Series对象,该参数通常无效,因为Series是一维的,所以轴总是0。

228-2-2、kind(可选,默认值为'quicksort')字符串,指定排序算法,可选值包括:

  • 'quicksort':快速排序,默认值。
  • 'mergesort':归并排序,稳定的排序算法。
  • 'heapsort':堆排序,不稳定的排序算法。

228-2-3、order(可选,默认值为None)在DataFrame中用于指定排序的列顺序;对于Series对象,此参数通常没有作用。

228-2-4、stable(可选,默认值为None)是否使用稳定排序算法,稳定排序算法保持相等元素的原始相对顺序,若设置为True,会使用稳定排序算法,否则会使用不稳定排序算法。

228-3、功能

        返回的是一个与原Series具有相同长度的整数序列,这些整数表示元素在排序后的索引位置。换句话说,返回的序列中每个值是原序列中对应元素的排序位置。

228-4、返回值

        返回一个Series对象,其中包含原始Series中每个元素的排序索引。

228-5、说明

        无

228-6、用法
228-6-1、数据准备
228-6-2、代码示例
# 228、pandas.Series.argsort方法
import pandas as pd
s = pd.Series([3, 6, 5, 11, 10, 8, 10, 24])
sorted_indices = s.argsort()
print(sorted_indices)
228-6-3、结果输出
# 228、pandas.Series.argsort方法
# 0    0
# 1    2
# 2    1
# 3    5
# 4    4
# 5    6
# 6    3
# 7    7
# dtype: int64
229、pandas.Series.argmin方法
229-1、语法
# 229、pandas.Series.argmin方法
pandas.Series.argmin(axis=None, skipna=True, *args, **kwargs)
Return int position of the smallest value in the Series.

If the minimum is achieved in multiple locations, the first row position is returned.

Parameters:
axis
{None}
Unused. Parameter needed for compatibility with DataFrame.

skipna
bool, default True
Exclude NA/null values when showing the result.

*args, **kwargs
Additional arguments and keywords for compatibility with NumPy.

Returns:
int
Row position of the minimum value.
229-2、参数

229-2-1、axis(可选,默认值为None)整数,指定轴进行排序,对于Series对象,该参数通常无效,因为Series是一维的,所以轴总是0。

229-2-2、skipna(可选,默认值为True)是否忽略缺失值(NaN),如果设置为True,则在寻找最小值时会跳过NaN值;如果为False,且存在NaN,则结果会是NaN。

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

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

229-3、功能

        返回的是Series中最小值的索引,如果Series中包含多个最小值,返回第一个最小值的索引。

229-4、返回值

        返回一个整数值,表示最小值在原始Series中的索引位置。

229-5、说明

        无

229-6、用法
229-6-1、数据准备
229-6-2、代码示例
# 229、pandas.Series.argmin方法
import pandas as pd
import numpy as np
s = pd.Series([3, 1, 2, np.nan, 4])
min_index = s.argmin()
print(min_index)
229-6-3、结果输出
# 229、pandas.Series.argmin方法
# 1
230、pandas.Series.argmax方法
230-1、语法
# 230、pandas.Series.argmax方法
pandas.Series.argmax(axis=None, skipna=True, *args, **kwargs)
Return int position of the largest value in the Series.

If the maximum is achieved in multiple locations, the first row position is returned.

Parameters:
axis
{None}
Unused. Parameter needed for compatibility with DataFrame.

skipna
bool, default True
Exclude NA/null values when showing the result.

*args, **kwargs
Additional arguments and keywords for compatibility with NumPy.

Returns:
int
Row position of the maximum value.
230-2、参数

230-2-1、axis(可选,默认值为None)整数,指定轴进行排序,对于Series对象,该参数通常无效,因为Series是一维的,所以轴总是0。

230-2-2、skipna(可选,默认值为True)是否忽略缺失值(NaN),如果设置为True,则在寻找最小值时会跳过NaN值;如果为False,且存在NaN,则结果会是NaN。

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

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

230-3、功能

        返回的是Series中最大值的索引,如果Series中包含多个最大值,返回第一个最大值的索引。

230-4、返回值

        返回一个整数值,表示最大值在原始Series中的索引位置。

230-5、说明

        无

230-6、用法
230-6-1、数据准备
230-6-2、代码示例
# 230、pandas.Series.argmax方法
import pandas as pd
import numpy as np
s = pd.Series([3, 1, 4, np.nan, 2])
max_index = s.argmax()
print(max_index)
230-6-3、结果输出
# 230、pandas.Series.argmax方法
# 2

二、推荐阅读

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1970140.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Python机器学习】Logistic回归——从疝气病症预测病马的死亡率

用Logistic回归来预测患有疝病的马的存活问题。这里的数据包括368个样本和28个特征。疝病是描述马肠胃痛的术语,这种病并不一定源自马的肠胃问题。 该数据集中包含了医院检测马疝病的一些指标,有些指标比较主观,有的指标难以测量&#xff0c…

docker部署elasticsearch和Kibana

部署elasticsearch 通过下面的Docker命令即可安装单机版本的elasticsearch: docker run -d \--name es \-e "ES_JAVA_OPTS-Xms512m -Xmx512m" \-e "discovery.typesingle-node" \-v es-data:/usr/share/elasticsearch/data \-v es-plugins:/u…

【STC32G12K128开发板】第3-9讲:手势识别(基于PAJ7620U2)

第3-9讲:手势识别(基于PAJ7620U2) 学习目的了解IK-PAJ7620U2手势识别传感器模块的功能。掌握IK-PAJ7620U2的I2C协议、操作流程,并编程实现配置IK-PAJ7620U2工作于接近检测和手势识别模式以及读取检测结果。 PAJ7620手势识别模块 产…

灰狼优化算法(GWO)的详细解读

一、引言 在优化问题中,我们常常需要寻找一个最优解,使得某个目标函数达到最小或最大值。为了高效地解决这类问题,研究者们从自然界中的生物行为汲取灵感,提出了多种群智能优化算法。灰狼优化算法(Grey Wolf Optimize…

行为验证码的介绍

1.什么是行为验证码 行为式验证码是一种较为流行的验证码。从字面来理解,就是通过用户的操作行为来完成验证,而无需去读懂扭曲的图片文字。常见的有两种:拖动式与点触式。 2.行为验证码的概念 行为式验证的核心思想是利用用户的“行为特征”…

单火供电零线发生器 单火变零火线开关面板零火开关老房改造必备

创作 史新华 零线发生器套件与单火线供电套件,作为现代智能家居解决方案中的创新之作,它们犹如智能电气领域的魔术师,巧妙地解决了传统智能开关在单火线路环境中因无零线而难以应用的难题。这些套件,如同智能电气世界的桥梁&…

SQLite库笔记:命令行shell

SQLite项目提供了一个简单的命令行程序sqlite3,它允许用户对SQLite数据库手动输入和执行SQL语句。更多详情可参考官网(https://www.sqlite.org/cli.html)。 help SQLite shell命令的help信息如下: .auth ON|OFF Sho…

卷积神经网络 - 动机(Motivation)篇

序言 在深度学习的浩瀚星空中,卷积神经网络( Convolutional Neural Networks, CNNs \text{Convolutional Neural Networks, CNNs} Convolutional Neural Networks, CNNs)无疑是最为璀璨的一颗星,其诞生与崛起深刻改变了图像识别、…

线程池的优势与应用

线程池的优势与应用 1、线程池的优势2、应用场景 💖The Begin💖点点关注,收藏不迷路💖 1、线程池的优势 资源复用:减少线程创建和销毁的开销,通过重用已存在的线程来提高效率。控制并发:有效管…

Solana公链

Solana 链的优势 Solana之所以能够实现高性能,主要是因为它采用了多种创新的技术和设计决策。下面是Solana能够达到高吞吐量、低延迟和低成本的一些关键因素: 1. 历史证明 (Proof of History, PoH): Solana引入了一种独特的共识机制&#…

【C++题解】1022. 百钱百鸡问题

欢迎关注本专栏《C从零基础到信奥赛入门级(CSP-J)》 问题:1022. 百钱百鸡问题 类型:嵌套穷举 题目描述: 用 100 元钱买 100 只鸡,公鸡,母鸡,小鸡都要有。 公鸡 5 元 1 只&#x…

【Kubernetes】kubeadmu快速部署k8s集群

目录 一.组件部署 二.环境初始化 三.所有节点部署docker,以及指定版本的kubeadm 四.所有节点安装kubeadm,kubelet和kubectl 五.高可用配置 六.部署K8S集群 1.master01 节点操作 2.master02、master03节点 3.master01 节点 4.master02、master…

酒店管理小程序的设计

管理员账户功能包括:系统首页,个人中心,用户管理,酒店管理员管理,房间类型管理,房间信息管理,订单信息管理,系统管理 微信端账号功能包括:系统首页,房间信息…

29-《夹竹桃》

夹竹桃 夹竹桃(学名:Nerium indicum Mill.)夹竹桃族夹竹桃属常绿直立大灌木,高可达5米,枝条灰绿色,嫩枝条具棱,被微毛,老时毛脱落。叶3-4枚轮生,叶面深绿,叶背…

Python可视化开发全面教程

Python是一种功能强大且易于学习的编程语言,它还提供了丰富的可视化库,如Matplotlib、Seaborn、Plotly和Bokeh。这些库使得数据可视化变得简单而直观。在本教程中,我们将介绍如何使用Python进行数据可视化,从基础知识到高级技巧。…

AI Agents(智能代理)教程:如何创建信息检索聊天机器人

AI 代理教程:如何创建信息检索聊天机器人 介绍 在本教程中,我们将指导您使用 AI 代理创建用于信息检索的复杂聊天机器人的过程。探索如何利用 AI 的强大功能构建能够高效地从各种来源检索数据的聊天机器人。 设置环境 我们的计划是使用 AI 代理&…

智慧教室建设方案

智慧教室建设方案摘要: 智慧教室发展和现状 智慧教室是教育现代化的重要体现,它经历了传统教学、多媒体教学、信息化教学等阶段。智慧教室利用先进的技术和理念,实现了教学环境的升级,包括本地和网络中控、远程管理、常态录播监控…

[渗透测试学习] PermX-HackTheBox

文章目录 PermX-HackTheBox信息搜集漏洞利用权限提升参考文章PermX-HackTheBox 信息搜集 nmap扫描一下端口 nmap -sC -v 10.10.11.23扫描结果如下 PORT STATE SERVICE 22/tcp open ssh | ssh-hostkey: | 256 e2:5c:5d:8c:47:3e:d8:72:f7:b4:80:03:49:86:6d:ef (ECDSA…

【系统架构设计师】二十二、嵌入式系统架构设计理论与实践③

目录 一、鸿蒙操作系统架构案例分析 1.1 鸿蒙操作系统定义 1.2 鸿蒙的层次化分析 1.2.1 内核层 1.2.2 系统服务层 1.2.3 框架层 1.2.4 应用层 1.3 鸿蒙操作系统的架构分析 1.3.1 鸿蒙操作系统架构具有4个技术特性 1.3.2 分布式架构所带来的优势 1.3.3 HarmonyOS 架构…

康耐视VisionPro GigE相机设置两种方法详细步骤

VisionPro GigE 相机设置方法一 1:关闭防火墙 2:打开 GigE Vision Configuration Tool 2.1:设置网卡巨型帧或者叫大型数据包 2.2:设置网卡 IP、掩码 2.3:设置相机 IP、掩码 VisionPro GigE 相机设置方法二 1:关闭防火墙 2:设置网卡 IP 跟掩码