Python酷库之旅-第三方库Pandas(056)
目录
一、用法精讲
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.NoteThe 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 = TrueReturns:
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 onNew 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 onNew 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、博客个人主页
相关文章:

Python酷库之旅-第三方库Pandas(056)
目录 一、用法精讲 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、功能…...

ZBrush入门使用介绍——4、笔刷选项说明
大家好,我是阿赵。 这次来看看ZBrush的笔刷的选项用法。 一、选择笔刷 点击笔刷,可以打开笔刷选择面板。 在最上面的Quick Pick,有最近使用过的笔刷,可以快速的选择。下面有很多可以选择的笔刷。但由于笔刷太多,…...

Java每日一练,技术成长不间断
目录 题目1.下列关于继承的哪项叙述是正确的?2.Java的跨平台特性是指它的源代码可以在多个平台运行。()3.以下 _____ 不是 Object 类的方法4.以下代码:5.下面哪个流类不属于面向字符的流()总结 题目 选自牛…...

传知代码-上下位关系自动检测方法(论文复现)
代码以及视频讲解 本文所涉及所有资源均在传知代码平台可获取 概述 本文复现论文 Hearst patterns revisited: Automatic hypernym detection from large text corpora[1] 提出的文本中上位词检测方法。 在自然语言处理中,上下位关系(Is-a Relations…...

从零开始的MicroPython(二) GPIO及代码应用
上一篇:http://t.csdnimg.cn/mg2Qt 文章目录 ESP32(NodeMCU-32S)简介引脚注意事项 类与对象的概念MicroPython的GPIO使用文档解释machine.PinPin.irq 点灯 ESP32(NodeMCU-32S) 简介 NodeMCU-32S 是安信可基于 ESP32-32S 模组所设计的核心开发板。该开发板延续了 N…...

嵌入式day15
数组指针 能够指向整个数组 一维数组: &a,考察a的数据类型 int(*p)[10]:表示一个指向长度为10的一维整型数组的指针 二维数组: 指向函数的指针 函数的函数名,即为函数的入口地址&#x…...

【电池管理系统(BMS)-01】 | 电池管理系统简介,动力电池和储能电池区别
🎩 欢迎来到技术探索的奇幻世界👨💻 📜 个人主页:一伦明悦-CSDN博客 ✍🏻 作者简介: C软件开发、Python机器学习爱好者 🗣️ 互动与支持:💬评论 &…...
C++ STL partial_sum 用法
一:功能 计算部分和,即遍历序列中每个元素,计算前 i 个元素的累加和,并将结果存在 i 的位置上。 二:用法 #include <iostream> #include <vector> #include <numeric>int main() {std::vector<…...

诚宜开张圣听不应妄自菲薄
拾人牙慧孜孜不倦 青山依旧在几度夕阳红朝闻道夕死可矣 青山依旧在几度夕阳红 安能以血补天我计不成乃天命也臣本布衣躬耕南阳大丈夫宁死不辱尔要试我宝剑是否锋利吗又待怎样休教天下人负我竖子不足与谋皇天不佑天下英雄唯使君与操尔青光殷殷其灿如炎备不量力欲申大义于天下我…...

Vue3 加载条(LoadingBar)
效果如下图:在线预览 APIs LoadingBar 参数说明类型默认值必传containerClass加载条容器的类名stringundefinedfalsecontainerStyle加载条容器的样式CSSProperties{}falseloadingBarSize加载条大小,单位 pxnumber2falsecolorLoading加载中颜色string‘…...

《CSS创意项目实战指南》:点亮网页,从实战中掌握CSS的无限创意
CSS创意项目实战指南 在数字时代,网页不仅是信息的载体,更是艺术与技术的融合体。通过CSS,你可以将平凡的网页转变为引人入胜的视觉盛宴,让用户体验跃升至全新高度。《CSS创意项目实战指南》正是这样一本引领你探索CSS无限可能的…...

[FBCTF2019]RCEService (PCRE回溯绕过和%a0换行绕过)
json格式输入ls出现index.php 这道题原本是给了源码的,BUUCTF没给 源码: <?phpputenv(PATH/home/rceservice/jail);if (isset($_REQUEST[cmd])) {$json $_REQUEST[cmd];if (!is_string($json)) {echo Hacking attempt detected<br/><br/…...

vue3后台管理系统 vue3+vite+pinia+element-plus+axios上
前言 项目安装与启动 使用vite作为项目脚手架 # pnpm pnpm create vite my-vue-app --template vue安装相应依赖 # sass pnpm i sass # vue-router pnpm i vue-router # element-plus pnpm i element-plus # element-plus/icon pnpm i element-plus/icons-vue安装element-…...

Mysql的事务隔离级别实现原理
一、事务隔离级别 mysql支持四种事务隔离级别: 读未提交:一个事务可以读取到另一个事务还未提交的数据;读已提交:一个事务可以读取到另一个事务已经提交的数据;可重复读:同一个事务中,无论读取…...

计算机体系结构:缓存一致性ESI
集中式缓存处理器结构(SMP) 不同核访问存储器时间相同。 分布式缓存处理器结构(NUMA) 共享存储器按模块分散在各处理器附近,处理器访问本地存储器和远程存储器的延迟不同,共享数据可进入处理器私有高速缓存…...

log4j2漏洞练习(未完成)
log4j2 是Apache的一个java日志框架,我们借助它进行日志相关操作管理,然而在2021年末log4j2爆出了远程代码执行漏洞,属于严重等级的漏洞。apache log4j通过定义每一条日志信息的级别能够更加细致地控制日志生成地过程,受影响的版本…...
常见网络攻击方法原理、应用场景和防御方法(一)
目录 1、SQL注入(SQL Injection)原理应用场景防御方法 2、跨站脚本攻击(XSS,Cross-Site Scripting)原理应用场景防御方法 3、跨站请求伪造(CSRF,Cross-Site Request Forgery)原理应用场景防御方法 4、文件上传漏洞原理应用场景防御方法 5、远程代码执行(…...

【leetcode十分钟】覆盖所有点的最少矩形数目(C++思路详解)
思路详解: 0. 题目情境并未限制矩形高度,故矩形数目的判断只和点的横坐标有关 1. 为了不重不漏地考虑到所有点,故笔者选择首先将二维数组中的点按横坐标的大小排序 //说明:本来笔者以为需要自定义sort排序,后来发现…...

【Vue3】默认插槽
【Vue3】默认插槽 背景简介开发环境开发步骤及源码 背景 随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内…...

华清day4 24-7-31
1> 使用父子进程完成两个文件的拷贝 父进程拷贝前一半内容,子进程拷贝后一半内容 子进程结束后退出,父进程回收子进程的资源 /* 使用父子进程完成两个文件的拷贝父进程拷贝前一半内容,子进程拷贝后一半内容 子进程结束后退出ÿ…...

Chapter03-Authentication vulnerabilities
文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

XCTF-web-easyupload
试了试php,php7,pht,phtml等,都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接,得到flag...
AtCoder 第409场初级竞赛 A~E题解
A Conflict 【题目链接】 原题链接:A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串,只有在同时为 o 时输出 Yes 并结束程序,否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...
oracle与MySQL数据库之间数据同步的技术要点
Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异,它们的数据同步要求既要保持数据的准确性和一致性,又要处理好性能问题。以下是一些主要的技术要点: 数据结构差异 数据类型差异ÿ…...

MODBUS TCP转CANopen 技术赋能高效协同作业
在现代工业自动化领域,MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步,这两种通讯协议也正在被逐步融合,形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

NFT模式:数字资产确权与链游经济系统构建
NFT模式:数字资产确权与链游经济系统构建 ——从技术架构到可持续生态的范式革命 一、确权技术革新:构建可信数字资产基石 1. 区块链底层架构的进化 跨链互操作协议:基于LayerZero协议实现以太坊、Solana等公链资产互通,通过零知…...

图表类系列各种样式PPT模版分享
图标图表系列PPT模版,柱状图PPT模版,线状图PPT模版,折线图PPT模版,饼状图PPT模版,雷达图PPT模版,树状图PPT模版 图表类系列各种样式PPT模版分享:图表系列PPT模板https://pan.quark.cn/s/20d40aa…...
【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索(基于物理空间 广播范围)2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...

使用 SymPy 进行向量和矩阵的高级操作
在科学计算和工程领域,向量和矩阵操作是解决问题的核心技能之一。Python 的 SymPy 库提供了强大的符号计算功能,能够高效地处理向量和矩阵的各种操作。本文将深入探讨如何使用 SymPy 进行向量和矩阵的创建、合并以及维度拓展等操作,并通过具体…...
动态 Web 开发技术入门篇
一、HTTP 协议核心 1.1 HTTP 基础 协议全称 :HyperText Transfer Protocol(超文本传输协议) 默认端口 :HTTP 使用 80 端口,HTTPS 使用 443 端口。 请求方法 : GET :用于获取资源,…...