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

news2024/9/17 8:47:11

目录

一、用法精讲

211、pandas.Series.truncate方法

211-1、语法

211-2、参数

211-3、功能

211-4、返回值

211-5、说明

211-6、用法

211-6-1、数据准备

211-6-2、代码示例

211-6-3、结果输出

212、pandas.Series.where方法

212-1、语法

212-2、参数

212-3、功能

212-4、返回值

212-5、说明

212-6、用法

212-6-1、数据准备

212-6-2、代码示例

212-6-3、结果输出

213、pandas.Series.mask方法

213-1、语法

213-2、参数

213-3、功能

213-4、返回值

213-5、说明

213-6、用法

213-6-1、数据准备

213-6-2、代码示例

213-6-3、结果输出

214、pandas.Series.add_prefix方法

214-1、语法

214-2、参数

214-3、功能

214-4、返回值

214-5、说明

214-6、用法

214-6-1、数据准备

214-6-2、代码示例

214-6-3、结果输出

215、pandas.Series.add_suffix方法

215-1、语法

215-2、参数

215-3、功能

215-4、返回值

215-5、说明

215-6、用法

215-6-1、数据准备

215-6-2、代码示例

215-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

211、pandas.Series.truncate方法
211-1、语法
# 211、pandas.Series.truncate方法
pandas.Series.truncate(before=None, after=None, axis=None, copy=None)
Truncate a Series or DataFrame before and after some index value.

This is a useful shorthand for boolean indexing based on index values above or below certain thresholds.

Parameters:
beforedate, str, int
Truncate all rows before this index value.

afterdate, str, int
Truncate all rows after this index value.

axis{0 or ‘index’, 1 or ‘columns’}, optional
Axis to truncate. Truncates the index (rows) by default. For Series this parameter is unused and defaults to 0.

copybool, default is True,
Return a copy of the truncated section.

Note

The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.

You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True

Returns:
type of caller
The truncated Series or DataFrame.
211-2、参数

211-2-1、before(可选,默认值为None)截取的起始位置,包含此索引值。如果未指定,则从Series的第一个索引开始。

211-2-2、after(可选,默认值为None)截取的结束位置,包含此索引值。如果未指定,则截取到Series的最后一个索引。

211-2-3、axis(可选,默认值为None)未使用,保留参数。

211-2-4、copy(可选,默认值为None)是否复制返回的数据。如果为False,则返回的Series是原始数据的视图,而不是副本。

211-3、功能

        用于截取Series对象的一部分数据,通常用于在时间序列数据或带有特定索引的数据集中选取特定范围的数据。

211-4、返回值

        返回一个pandas.Series对象,包含在before和after参数指定的范围内的元素。

211-5、说明

        无

211-6、用法
211-6-1、数据准备
211-6-2、代码示例
# 211、pandas.Series.truncate方法
# 211-1、截取指定范围的数据
import pandas as pd
s = pd.Series([10, 20, 30, 40, 50, 60, 70, 80], index=[1, 2, 3, 4, 5, 6, 7, 8])
result = s.truncate(before=3, after=6)
print(result, end='\n\n')

# 211-2、截取从索引4到最后的数据
import pandas as pd
s = pd.Series([10, 20, 30, 40, 50, 60, 70, 80], index=[1, 2, 3, 4, 5, 6, 7, 8])
result = s.truncate(before=4)
print(result, end='\n\n')

# 211-3、截取从开头到索引5的数据
import pandas as pd
s = pd.Series([10, 20, 30, 40, 50, 60, 70, 80], index=[1, 2, 3, 4, 5, 6, 7, 8])
result = s.truncate(after=5)
print(result)
211-6-3、结果输出
# 211、pandas.Series.truncate方法
# 211-1、截取指定范围的数据
# 3    30
# 4    40
# 5    50
# 6    60
# dtype: int64

# 211-2、截取从索引4到最后的数据
# 4    40
# 5    50
# 6    60
# 7    70
# 8    80
# dtype: int64

# 211-3、截取从开头到索引5的数据
# 1    10
# 2    20
# 3    30
# 4    40
# 5    50
# dtype: int64
212、pandas.Series.where方法
212-1、语法
# 212、pandas.Series.where方法
pandas.Series.where(cond, other=nan, *, inplace=False, axis=None, level=None)
Replace values where the condition is False.

Parameters:
cond
bool Series/DataFrame, array-like, or callable
Where cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it).

other
scalar, Series/DataFrame, or callable
Entries where cond is False are replaced with corresponding value from other. If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn’t check it). If not specified, entries will be filled with the corresponding NULL value (np.nan for numpy dtypes, pd.NA for extension dtypes).

inplace
bool, default False
Whether to perform the operation in place on the data.

axis
int, default None
Alignment axis if needed. For Series this parameter is unused and defaults to 0.

level
int, default None
Alignment level if needed.

Returns:
Same type as caller or None if
inplace=True.
212-2、参数

212-2-1、cond(必须)布尔型数组,条件表达式或Series,它用于指定要保留的元素。当条件为True时保留原值,为False时替换为other的值。

212-2-2、other(可选,默认值为nan)用来替换不满足条件的元素的值,默认情况下,这些元素会被替换为NaN。

212-2-3、inplace(可选,默认值为False)如果为True,则在原地修改Series对象,而不是返回修改后的副本。

212-2-4、axis(可选,默认值为None)未使用,保留参数。

212-2-5、level(可选,默认值为None)如果Series是多层索引的,可以指定操作的索引层次。

212-3、功能

        用于基于条件对Series数据进行筛选和替换,它根据给定的布尔条件保留或替换Series中的值。

212-4、返回值

        返回一个pandas.Series对象,其中原来的数据根据cond条件被筛选和替换。

212-5、说明

        无

212-6、用法
212-6-1、数据准备
212-6-2、代码示例
# 212、pandas.Series.where方法
# 212-1、基本用法
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.where(s > 2)
print(result, end='\n\n')

# 212-2、指定替换值
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.where(s > 2, other=-1)
print(result, end='\n\n')

# 212-3、使用布尔条件
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.where(s % 2 == 0, other='odd')
print(result, end='\n\n')

# 212-4、原地修改
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
s.where(s > 2, other=-1, inplace=True)
print(s)
212-6-3、结果输出
# 212、pandas.Series.where方法
# 212-1、基本用法
# 0    NaN
# 1    NaN
# 2    3.0
# 3    4.0
# 4    5.0
# dtype: float64

# 212-2、指定替换值
# 0   -1
# 1   -1
# 2    3
# 3    4
# 4    5
# dtype: int64

# 212-3、使用布尔条件
# 0    odd
# 1      2
# 2    odd
# 3      4
# 4    odd
# dtype: object

# 212-4、原地修改
# 0   -1
# 1   -1
# 2    3
# 3    4
# 4    5
# dtype: int64
213、pandas.Series.mask方法
213-1、语法
# 213、pandas.Series.mask方法
pandas.Series.mask(cond, other=_NoDefault.no_default, *, inplace=False, axis=None, level=None)
Replace values where the condition is True.

Parameters:
cond
bool Series/DataFrame, array-like, or callable
Where cond is False, keep the original value. Where True, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it).

other
scalar, Series/DataFrame, or callable
Entries where cond is True are replaced with corresponding value from other. If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn’t check it). If not specified, entries will be filled with the corresponding NULL value (np.nan for numpy dtypes, pd.NA for extension dtypes).

inplace
bool, default False
Whether to perform the operation in place on the data.

axis
int, default None
Alignment axis if needed. For Series this parameter is unused and defaults to 0.

level
int, default None
Alignment level if needed.

Returns:
Same type as caller or None if
inplace=True.
213-2、参数

213-2-1、cond(必须)布尔型数组,条件表达式或Series,它用于指定要替换的元素。当条件为True时替换为other的值,为False时保留原值。

213-2-2、other(可选)用来替换满足条件的元素的值,默认情况下,这些元素会被替换为NaN。

213-2-3、inplace(可选,默认值为False)如果为True,则在原地修改Series对象,而不是返回修改后的副本。

213-2-4、axis(可选,默认值为None)未使用,保留参数。

213-2-5、level(可选,默认值为None)如果Series是多层索引的,可以指定操作的索引层次。

213-3、功能

        用于替换Series数据中的元素。如果满足指定的条件(cond),则将这些元素替换为other的值;否则,保留原值。

213-4、返回值

        返回一个pandas.Series对象,其中原来的数据根据cond条件被筛选和替换。

213-5、说明

        无

213-6、用法
213-6-1、数据准备
213-6-2、代码示例
# 213、pandas.Series.mask方法
# 213-1、基本用法
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.mask(s > 2)
print(result, end='\n\n')

# 213-2、指定替换值
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.mask(s > 2, other=-1)
print(result, end='\n\n')

# 213-3、使用布尔条件
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.mask(s % 2 == 0, other='even')
print(result, end='\n\n')

# 213-4、原地修改
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
s.mask(s > 2, other=-1, inplace=True)
print(s)
213-6-3、结果输出
# 213、pandas.Series.mask方法
# 213-1、基本用法
# 0    1.0
# 1    2.0
# 2    NaN
# 3    NaN
# 4    NaN
# dtype: float64

# 213-2、指定替换值
# 0    1
# 1    2
# 2   -1
# 3   -1
# 4   -1
# dtype: int64

# 213-3、使用布尔条件
# 0       1
# 1    even
# 2       3
# 3    even
# 4       5
# dtype: object

# 213-4、原地修改
# 0    1
# 1    2
# 2   -1
# 3   -1
# 4   -1
# dtype: int64
214、pandas.Series.add_prefix方法
214-1、语法
# 214、pandas.Series.add_prefix方法
pandas.Series.add_prefix(prefix, axis=None)
Prefix labels with string prefix.

For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed.

Parameters:
prefixstr
The string to add before each label.

axis{0 or ‘index’, 1 or ‘columns’, None}, default None
Axis to add prefix on

New in version 2.0.0.

Returns:
Series or DataFrame
New Series or DataFrame with updated labels.
214-2、参数

214-2-1、prefix(必须)字符串类型,表示要添加到索引标签前的前缀。

214-2-2、axis(可选,默认值为None)虽然存在这个参数,但在Series中没有实际意义,因为Series没有轴的概念。

214-3、功能

        通过为Series的索引标签添加一个指定的前缀,返回一个新的Series对象。

214-4、返回值

        返回一个pandas.Series对象,其索引标签被添加了指定的前缀。

214-5、说明

        无

214-6、用法
214-6-1、数据准备
214-6-2、代码示例
# 214、pandas.Series.add_prefix方法
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
result = s.add_prefix('item_')
print(result)
214-6-3、结果输出
# 214、pandas.Series.add_prefix方法
# item_a    1
# item_b    2
# item_c    3
# dtype: int64
215、pandas.Series.add_suffix方法
215-1、语法
# 215、pandas.Series.add_suffix方法
pandas.Series.add_suffix(suffix, axis=None)
Suffix labels with string suffix.

For Series, the row labels are suffixed. For DataFrame, the column labels are suffixed.

Parameters:
suffixstr
The string to add after each label.

axis{0 or ‘index’, 1 or ‘columns’, None}, default None
Axis to add suffix on

New in version 2.0.0.

Returns:
Series or DataFrame
New Series or DataFrame with updated labels.
215-2、参数

215-2-1、suffix(必须)字符串类型,表示要添加到索引标签后的后缀。

215-2-2、axis(可选,默认值为None)虽然存在这个参数,但在Series中没有实际意义,因为Series没有轴的概念。

215-3、功能

        用于为Series的索引标签添加一个后缀,和add_prefix类似,axis参数在Series中没有实际意义,因为Series是一维的。

215-4、返回值

        返回一个pandas.Series对象,其索引标签被添加了指定的后缀。

215-5、说明

        无

215-6、用法
215-6-1、数据准备
215-6-2、代码示例
# 215、pandas.Series.add_suffix方法
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
result = s.add_suffix('_item')
print(result)
215-6-3、结果输出
# 215、pandas.Series.add_suffix方法
# a_item    1
# b_item    2
# c_item    3
# dtype: int64

二、推荐阅读

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

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

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

相关文章

论报文加密加签场景下如何高效的进行渗透测试

前言 最新的测试中,经常遇到HTTP报文加密/加签传输的情况,这导致想要查看和修改明文报文很不方便。 之前应对这种情况我们有几种常见的办法解决,比如使用burpy插件、在Burp上下游使用mitmproxy进行代理等,但这些使用起来不太方便…

LSTM详解总结

LSTM(Long Short-Term Memory)是一种用于处理和预测时间序列数据的递归神经网络(RNN)的改进版本。其设计初衷是为了解决普通RNN在长序列训练中出现的梯度消失和梯度爆炸问题。以下是对LSTM的详细解释,包括原理、公式、…

面向非结构化数据的知迟抽取

文章目录 实体抽取关系抽取事件抽取大量的数据以非结构化数据(即自由文本)的形式存在,如新闻报道、科技文献和政府文件等,面向文本数据的知识抽取一直是广受关注的问题。在前文介绍的知识抽取领域的评测竞赛中,评测数据大多属于非结构化文本数据。本节将对这一类知识抽取技…

Prometheus-部署

Prometheus-部署 Server端安装配置部署Node Exporters监控系统指标监控MySQL数据库监控nginx安装grafana Server端安装配置 1、上传安装包,并解压 cd /opt/ tar xf prometheus-2.30.3.linux-amd64.tar.gz mv prometheus-2.30.3.linux-amd64 /usr/local/prometheus…

【音频识别】十大数据集合集,宝藏合集,不容错过!

本文将为您介绍10个经典、热门的数据集,希望对您在选择适合的数据集时有所帮助。 1 RenderMe-360 发布方: 上海人工智能实验室 发布时间: 2023-05-24 简介: RenFace是一个大规模多视角人脸高清视频数据集,包含多样的…

2024年最强网络安全学习路线,详细到直接上清华的教材!

关键词:网络安全入门、渗透测试学习、零基础学安全、网络安全学习路线 首先咱们聊聊,学习网络安全方向通常会有哪些问题前排提示:文末有CSDN官方认证Python入门资料包 ! 1、打基础时间太长 学基础花费很长时间,光语…

Redis内存管理

文章目录 Redis内存管理删除策略淘汰策略LRU算法 Redis内存管理 长期把Redis做缓存用,总有一天Redis内存总会满的。有没有思考过这个问题,Redis内存满了会怎么样?在redis.conf中把Redis内存设置为1个字节,做一个测试:…

【随机链表的复制】python刷题记录

R3-哈希表 参考k神题解 哈希表法: """ # Definition for a Node. class Node:def __init__(self, x: int, next: Node None, random: Node None):self.val int(x)self.next nextself.random random """class Solution:def copy…

“打破常规:评估八股文对工作的真正影响“

“八股文”在实际工作中是助力、阻力还是空谈? 作为现在各类大中小企业面试程序员时的必问内容,“八股文”似乎是很重要的存在。但“八股文”是否能在实际工作中发挥它“敲门砖”应有的作用呢?有IT人士不禁发出疑问:程序员面试考什…

基于深度学习的结肠炎严重度诊断

基于深度学习的结肠炎严重度诊断 本文所涉及所有资源均在传知代码平台可获取 文章目录 基于深度学习的结肠炎严重度诊断1.概述1.1 数据集展示1.2 Resnet50介绍1.2.1结构与特点1.2.2关键优势1.2.3总结 2.创新点3.结果可视化展示结果展示4.核心逻辑5.部署及使用方式5.1 环境配置5…

彻底搞清楚SSR同构渲染的首屏

作为.NET技术栈的全干工程师,Blazor、Vue/Nuxt.js和React/Next.js都会接触到。它们(准确的说是Blazor、Nuxt和Next),都实现了SSR同构渲染。要了解同构渲染,需要从服务端渲染开始。 传统的服务端渲染 如下图所示&…

开放式耳机什么牌子的好?看这6大品牌就够了

移动互联网时代,听歌、追剧、网课、短视频……这几年全球青年人对于耳机和耳朵的依赖程度,可谓前所未有的提升。但选择一款好的耳机,也不是一件容易的事,入耳式耳机戴久了耳道会疼,还可能引起一系列不必要的炎症&#…

【C语言】C语言期末突击/考研--数据的类型

目录 一、编程环境的搭建 二、数据的类型、数据的输入输出 2.1.数据类型 2.2.常量 2.3.变量 2.4.整型数据 2.4.1.符号常量 2.4.2.整型变量 2.5.浮点型数据 2.5.1.浮点型常量 2.5.2.浮点型变量 2.6.字符型数据 2.6.1字符型常量 2.6.2.字符数据在内存中的存储形式及…

Python 【机器学习】 进阶 之 【实战案例】房价数据中位数分析 | 1/3(含分析过程)

Python 【机器学习】 进阶 之 【实战案例】房价数据中位数分析 | 1/3(含分析过程) 目录 Python 【机器学习】 进阶 之 【实战案例】房价数据中位数分析 | 1/3(含分析过程) 一、简单介绍 二、机器学习 1、为什么使用机器学习&a…

react antd upload custom request处理多个文件上传

react antd upload custom request处理多个文件上传的问题 背景:第一次请求需要请求后端返回aws 一个link,再往link push文件,再调用另一个接口告诉后端已经上传成功,拿到返回值。 再把返回值传给业务api... 多文件上传一直是循环…

字体表绘制的理解

下载字体到项目根目录下,我们通过一些在写预览本地字体的网站,简单看一下 通过图片不难看出阴书与原文的对应关系,接下来通过程序去完成这一过程,通过 fonttools 处理 ttf,然后获取字体和文字对应的 xml 文件 下面简单…

分布式SQL查询引擎之ByConity

ByConity 是字节跳动面向现代数据栈的一款开源数仓系统,应用了大量数据库成熟技术,如列存引擎,MPP 执行,智能查询优化,向量化执行,Codegen,indexing,数据压缩,适合用于 O…

线程池和进程池,输出有区别吗?

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor def fn(name):for i in range(1000):print(name,i)if __name__ __main__:with ThreadPoolExecutor(10) as t:for i in range(100):t.submit(fn,namef"线程{i}")with ProcessPoolExecutor(10…

艾体宝干货 | 如何分析关键网络性能指标?持续接收样品试用申请!

网络性能是企业顺利运营的重要基础,而Allegro流量分析仪作为一款强大的网络性能分析工具,为企业提供了深入了解网络运行状况的途径。在本文中,我们将探讨如何利用Allegro 流量分析仪分析关键网络性能指标,以优化网络性能、提高安全…

【综合案例】使用DevEco Studio编写京东登录界面

效果展示 模块拆分 布局容器 顶部 Logo输入框登录区域底部模块区域 知识点 复选框 Checkbox一段文本多个样式:Text 包裹 SpanRow 或 Column 空白区域填充:Blank线性渐变背景: .linearGradient({angle: 135, // 设置颜色渐变起始角度为顺时针…