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

news2024/9/22 5:36:20

目录

一、用法精讲

151、pandas.Series.any方法

151-1、语法

151-2、参数

151-3、功能

151-4、返回值

151-5、说明

151-6、用法

151-6-1、数据准备

151-6-2、代码示例

151-6-3、结果输出

152、pandas.Series.autocorr方法

152-1、语法

152-2、参数

152-3、功能

152-4、返回值

152-5、说明

152-6、用法

152-6-1、数据准备

152-6-2、代码示例

152-6-3、结果输出

153、pandas.Series.between方法

153-1、语法

153-2、参数

153-3、功能

153-4、返回值

153-5、说明

153-6、用法

153-6-1、数据准备

153-6-2、代码示例

153-6-3、结果输出

154、pandas.Series.clip方法

154-1、语法

154-2、参数

154-3、功能

154-4、返回值

154-5、说明

154-6、用法

154-6-1、数据准备

154-6-2、代码示例

154-6-3、结果输出

155、pandas.Series.corr方法

155-1、语法

155-2、参数

155-3、功能

155-4、返回值

155-5、说明

155-6、用法

155-6-1、数据准备

155-6-2、代码示例

155-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

151、pandas.Series.any方法
151-1、语法
# 151、pandas.Series.any方法
pandas.Series.any(*, axis=0, bool_only=False, skipna=True, **kwargs)
Return whether any element is True, potentially over an axis.

Returns False unless there is at least one element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-empty).

Parameters:
axis
{0 or ‘index’, 1 or ‘columns’, None}, default 0
Indicate which axis or axes should be reduced. For Series this parameter is unused and defaults to 0.

0 / ‘index’ : reduce the index, return a Series whose index is the original column labels.

1 / ‘columns’ : reduce the columns, return a Series whose index is the original index.

None : reduce all axes, return a scalar.

bool_only
bool, default False
Include only boolean columns. Not implemented for Series.

skipna
bool, default True
Exclude NA/null values. If the entire row/column is NA and skipna is True, then the result will be False, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero.

**kwargs
any, default None
Additional keywords have no effect but might be accepted for compatibility with NumPy.

Returns:
scalar or Series
If level is specified, then, Series is returned; otherwise, scalar is returned.
151-2、参数

151-2-1、axis(可选,默认值为0)该参数在Series对象中没有实际意义,因为Series是一维的。

151-2-2、bool_only(可选,默认值为False)如果为True,则仅计算布尔值。

151-2-3、skipna(可选,默认值为True)如果为True,则跳过NA/null值。

151-2-4、**kwargs(可选)传递给函数的其他关键字参数。

151-3、功能

        检查Series对象中的是否存在至少一个True值,如果Series中至少有一个值为True,则返回True,否则返回False。

151-4、返回值

        返回一个布尔值,如果Series对象中的任意一个元素是True,则返回True,否则返回False。

151-5、说明

        应用场景:

151-5-1、数据验证:在数据分析过程中,可以用来验证某些条件是否在至少一个数据中满足。

151-5-2、条件检查:可以用于检查数据集中的是否存在任何一个元素符合特定条件。

151-5-3、数据清理:在数据清理过程中,检查数据集中是否存在任何非空值或者是否有任何数据满足特定标准。

151-6、用法
151-6-1、数据准备
151-6-2、代码示例
# 151、pandas.Series.any方法
import pandas as pd
data = pd.Series([False, False, True, 0, 1])
result = data.any()
print(result)
151-6-3、结果输出
# 151、pandas.Series.any方法
# True
152、pandas.Series.autocorr方法
152-1、语法
# 152、pandas.Series.autocorr方法
pandas.Series.autocorr(lag=1)
Compute the lag-N autocorrelation.

This method computes the Pearson correlation between the Series and its shifted self.

Parameters:
lag
int, default 1
Number of lags to apply before performing autocorrelation.

Returns:
float
The Pearson correlation between self and self.shift(lag).
152-2、参数

152-2-1、lag(可选,默认值为1)指定的滞后值,滞后值表示计算当前数据与之前多少个时间步的数据之间的相关性。

152-3、功能

        计算时间序列数据与其自身在指定滞后(lag)下的自相关系数。

152-4、返回值

        返回一个浮点数,表示时间序列在指定滞后下的自相关系数。

152-5、说明

        应用场景:

152-5-1、时间序列分析:自相关性分析是时间序列分析中的重要部分,帮助理解数据的模式和趋势。

152-5-2、预测模型:自相关性高的数据可能具有预测性,可以用于构建预测模型。

152-5-3、信号处理:在信号处理和控制系统中,自相关性用于分析信号的延迟效应。

152-6、用法
152-6-1、数据准备
152-6-2、代码示例
# 152、pandas.Series.autocorr方法
import pandas as pd
data = pd.Series([0.1, 0.4, 0.3, 0.7, 0.9])
result = data.autocorr(lag=1)
print(result)
152-6-3、结果输出
# 152、pandas.Series.autocorr方法
# 0.6657502859356824
153、pandas.Series.between方法
153-1、语法
# 153、pandas.Series.between方法
pandas.Series.between(left, right, inclusive='both')
Return boolean Series equivalent to left <= series <= right.

This function returns a boolean vector containing True wherever the corresponding Series element is between the boundary values left and right. NA values are treated as False.

Parameters:
leftscalar or list-like
Left boundary.

rightscalar or list-like
Right boundary.

inclusive{“both”, “neither”, “left”, “right”}
Include boundaries. Whether to set each bound as closed or open.

Changed in version 1.3.0.

Returns:
Series
Series representing whether each element is between left and right (inclusive).
153-2、参数

153-2-1、left(必须)表示区间的左端点。

153-2-2、right(必须)表示区间的右端点。

153-2-3、inclusive(可选,默认值为'both')指定是否包含边界值,可以是以下三个值之一:

153-2-3-1、'both':包括left和right。

153-2-3-2、'neither':不包括left和right。

153-2-3-3、'left':包括left,但不包括right。

153-2-3-4、'right':包括right,但不包括left。

153-3、功能

        用于判断序列中的元素是否在指定的left和right值之间,并返回一个布尔序列。

153-4、返回值

        返回一个布尔类型的pandas.Series,其中每个值表示原序列中的相应值是否在指定的区间范围内。

153-5、说明

        应用场景:

153-5-1、数据筛选:快速筛选出符合条件的数值。

153-5-2、条件判断:在数据处理中进行复杂的条件判断和过滤。

153-5​​​​​​​-3、数据分析:分析数据时,筛选出在特定范围内的数据点

153-6、用法
153-6-1、数据准备
153-6-2、代码示例
# 153、pandas.Series.between方法
import pandas as pd
data = pd.Series([3, 5, 6, 8, 10, 11, 24])
result = data.between(5, 11, 'neither')
print(result)
153-6-3、结果输出
# 153、pandas.Series.between方法
# 0    False
# 1    False
# 2     True
# 3     True
# 4     True
# 5    False
# 6    False
# dtype: bool
154、pandas.Series.clip方法
154-1、语法
# 154、pandas.Series.clip方法
pandas.Series.clip(lower=None, upper=None, *, axis=None, inplace=False, **kwargs)
Trim values at input threshold(s).

Assigns values outside boundary to boundary values. Thresholds can be singular values or array like, and in the latter case the clipping is performed element-wise in the specified axis.

Parameters:
lower
float or array-like, default None
Minimum threshold value. All values below this threshold will be set to it. A missing threshold (e.g NA) will not clip the value.

upper
float or array-like, default None
Maximum threshold value. All values above this threshold will be set to it. A missing threshold (e.g NA) will not clip the value.

axis
{{0 or ‘index’, 1 or ‘columns’, None}}, default None
Align object with lower and upper along the given axis. For Series this parameter is unused and defaults to None.

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

*args, **kwargs
Additional keywords have no effect but might be accepted for compatibility with numpy.

Returns:
Series or DataFrame or None
Same type as calling object with the values outside the clip boundaries replaced or None if inplace=True.
154-2、参数

154-2-1、lower(可选,默认值为None)用于设置下界的标量值或数组,如果设置为None,则不应用下界。

154-2-2、upper(可选,默认值为None)用于设置上界的标量值或数组,如果设置为None,则不应用上界。

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

154-2-4、inplace(可选,默认值为False)如果设置为True,将直接在原序列上进行修改,而不是返回一个新的序列。

154-2-5、**kwargs(可选)其他关键字参数。

154-3、功能

        将序列中的值限制在指定的上下界范围内,如果某个值小于lower,则将其设置为lower;如果某个值大于upper,则将其设置为upper。

154-4、返回值

        返回一个新的pandas.Series,其中的值已被限制在指定范围内,如果inplace=True,则返回None。

154-5、说明

        应用场景:

154-5-1、处理异常值:限制数据中的极端值,防止其影响分析结果。

154-5-2、数据清理:将数据限制在合理范围内,确保数据质量。

154-5​​​​​​​-3、预处理:在机器学习中对数据进行预处理,确保输入数据在合理范围内。

154-6、用法
154-6-1、数据准备
154-6-2、代码示例
# 154、pandas.Series.clip方法
import pandas as pd
data = pd.Series([1, 2, 3, 4, 5])
clipped_data = data.clip(lower=2, upper=4)
print(clipped_data, end='\n\n')
data.clip(lower=2, upper=4, inplace=True)
print(data)
154-6-3、结果输出
# 154、pandas.Series.clip方法
# 0    2
# 1    2
# 2    3
# 3    4
# 4    4
# dtype: int64
#
# 0    2
# 1    2
# 2    3
# 3    4
# 4    4
# dtype: int64
155、pandas.Series.corr方法
155-1、语法
# 155、pandas.Series.corr方法
pandas.Series.corr(other, method='pearson', min_periods=None)
Compute correlation with other Series, excluding missing values.

The two Series objects are not required to be the same length and will be aligned internally before the correlation function is applied.

Parameters:
otherSeries
Series with which to compute the correlation.

method{‘pearson’, ‘kendall’, ‘spearman’} or callable
Method used to compute correlation:

pearson : Standard correlation coefficient

kendall : Kendall Tau correlation coefficient

spearman : Spearman rank correlation

callable: Callable with input two 1d ndarrays and returning a float.

Warning

Note that the returned matrix from corr will have 1 along the diagonals and will be symmetric regardless of the callable’s behavior.

min_periodsint, optional
Minimum number of observations needed to have a valid result.

Returns:
float
Correlation with other.
155-2、参数

155-2-1、other(必须)表示另一个与当前序列进行相关性计算的Series对象。

155-2-2、method(可选,默认值为'pearson')计算相关系数的方法,可选值包括:

155-2-2-1、'pearson':计算皮尔逊相关系数,这是最常用的相关系数,衡量线性关系。

155-2-2-2、'kendall':计算肯德尔等级相关系数,用于衡量序列间的等级相关性。

155-2-2-3、'spearman':计算斯皮尔曼等级相关系数,用于衡量序列间的单调关系。

155-2-3、min_periods(可选,默认值为None)最小有效观察数,要求计算相关系数的非NA(缺失值)数据点的最少数量,如果可用数据点少于这个值,将返回NaN。

155-3、功能

        用于计算两个序列之间的相关系数,相关系数用于衡量两个序列之间的线性关系。

155-4、返回值

        返回一个浮点数,表示两个序列之间的相关系数。

155-5、说明

        应用场景:

155-5-1、数据分析:评估两个变量之间的线性关系。

155-5-2、特征选择:在机器学习中,选择与目标变量高度相关的特征。

155-5​​​​​​​-3、金融分析:评估不同金融资产之间的关系。

155-6、用法
155-6-1、数据准备
155-6-2、代码示例
# 155、pandas.Series.corr方法
# 155-1、计算皮尔逊相关系数
import pandas as pd
data1 = pd.Series([1, 2, 3, 4, 5])
data2 = pd.Series([5, 4, 3, 2, 1])
corr_pearson = data1.corr(data2)
print(corr_pearson, end='\n\n')

# 155-2、计算斯皮尔曼等级相关系数
import pandas as pd
data1 = pd.Series([1, 2, 3, 4, 5])
data2 = pd.Series([5, 4, 3, 2, 1])
corr_spearman = data1.corr(data2, method='spearman')
print(corr_spearman, end='\n\n')

# 155-3、指定最小有效观察数
import pandas as pd
data1 = pd.Series([1, 2, 3, 4, None])
data2 = pd.Series([5, 4, 3, 2, 1])
corr_with_min_periods = data1.corr(data2, min_periods=4)
print(corr_with_min_periods)
155-6-3、结果输出
# 155、pandas.Series.corr方法
# 155-1、计算皮尔逊相关系数
# -0.9999999999999999

# 155-2、计算斯皮尔曼等级相关系数
# -0.9999999999999999

# 155-3、指定最小有效观察数
# -1.0

二、推荐阅读

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

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

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

相关文章

c++树(三)重心

目录 重心的基础概念 定义&#xff1a;使最大子树大小最小的点叫做树的重心 树的重心求解方式 例题&#xff1a; 重心的性质 性质1&#xff1a;重心点的最大子树大小不大于整棵树大小的一半。 性质1证明&#xff1a; 性质1的常用推导 推导1&#xff1a; 推导2&#x…

《Milvus Cloud向量数据库指南》——开源许可证的范围:深入解析与选择指南

在开源软件的广阔天地中,开源许可证作为连接开发者与用户之间的重要法律桥梁,其类型多样且各具特色。每一种许可证都精心设计了特定的权限、限制和要求,旨在保护创作者的权益,同时促进软件的创新与共享。对于开发者和用户而言,深入理解并恰当选择适合的开源许可证,是确保…

C++树(四)二叉树

目录 二叉树的定义&#xff1a; 二叉树相关术语&#xff1a; 二叉树的概念与性质 二叉树基本性质 二叉树的节点数量 满二叉树概念&#xff1a; 完全二叉树概念&#xff1a; 完全二叉树性质&#xff1a; 二叉树的存储 二叉树的遍历 在此基础上&#xff0c;二叉树的遍历…

mac下010editor的配置文件路径

1.打开访达&#xff0c;点击前往&#xff0c;输入~/.config 2.打开这个文件夹 把里面的 010 Editor.ini 文件删除即可&#xff0c;重新安装010 Editor即可

有没有下面符合以下条件的电子时钟的代码

&#x1f3c6;本文收录于《CSDN问答解答》专栏&#xff0c;主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&收藏&…

【React1】React概述、基本使用、脚手架、JSX、组件

文章目录 1. React基础1.1 React 概述1.1.1 什么是React1.1.2 React 的特点声明式基于组件学习一次,随处使用1.2 React 的基本使用1.2.1 React的安装1.2.2 React的使用1.2.3 React常用方法说明React.createElement()ReactDOM.render()1.3 React 脚手架的使用1.3.1 React 脚手架…

PostgreSQL使用(四)——数据查询

说明&#xff1a;对于一门SQL语言&#xff0c;数据查询是我们非常常用的&#xff0c;也是SQL语言中非常大的一块。本文介绍PostgreSQL使用中的数据查询&#xff0c;如有一张表&#xff0c;内容如下&#xff1a; 简单查询 --- 1.查询某张表的全部数据 select * from tb_student…

MSPM0G3507基于keil无法烧录的解决方法

在学习M0的板卡过程中&#xff0c;遇到了诸多玄学问题。网上的教学大多基于CCS开发&#xff0c;对keil的教学几乎没有。 一开始我以为这个问题是没添加这个&#xff0c;但其实并非如此 在群里的网友说的清除flash&#xff0c;插拔USB,这些都不管用,后面也发现先在CCS烧录一遍&…

前端开发知识(二)-css

<head> <style> div{ } </style> </head> div是布局标签&#xff0c; 一般放在head标签内&#xff0c;最下部。 若直接在在.css文件中写css,文件中&#xff0c;直接写就行&#xff0c;如下所示。 div{ }

VLLM代码解读 | VLLM Hack 3

在上一期&#xff0c;我们看到了多个输入如何被封装&#xff0c;然后被塞入llm_engine中&#xff0c;接下来&#xff0c;通过_run_engine,我们要进行输入的处理了。 def _run_engine(self, *, use_tqdm: bool) -> List[Union[RequestOutput, EmbeddingRequestOutput]]:# Ini…

java-poi实现excel自定义注解生成数据并导出

因为项目很多地方需要使用导出数据excel的功能&#xff0c;所以开发了一个简易的统一生成导出方法。 依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version…

【LeetCode】201. 数字范围按位与

1. 题目 2. 分析 这题挺难想的&#xff0c;我到现在还没想明白&#xff0c;为啥只用左区间和右区间就能找到目标值了&#xff0c;而不用挨个做与操作&#xff1f; 3. 代码 class Solution:def rangeBitwiseAnd(self, left: int, right: int) -> int:left_bin bin(left).…

五. TensorRT API的基本使用-TensorRT-network-structure

目录 前言0. 简述1. 案例运行2. 代码分析2.1 main.cpp2.2 model.cpp 总结下载链接参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战课程》&#xff0c;链接。记录下个人学习笔记&#xff0c;仅供自己参考 本次课程我们来学习课程第五章—TensorRT API 的基本使用&#x…

java面向对象进阶进阶篇--《接口和接口与抽象类综合案例》(附带全套源代码)

个人主页→VON 收录专栏→java从入门到起飞 抽象类→抽象类和抽象方法 目录 一、初识接口 特点和用途 示例&#xff1a; Animal类 Dog类 Frog类 Rabbit类 Swim接口 text测试类 结果展示&#xff1a; 二、接口的细节 接口中的成员特点&#xff1a; 成员特点与接口的关…

【通信模块】WiFi&Bluetooth简介与对比

学习云里物里科技文章及结合CSDN优秀作者Edison Tao总结笔记&#xff0c;侵权联删&#xff01; 云里物里科技&#xff1a; https://www.minewtech.com/news/industry-2019-01-25-01.html CSDN&#xff1a; https://blog.csdn.net/taotongning/article/details/95215927 WIFI…

EXCEL 排名(RANK,COUNTIFS)

1.单列排序 需求描述&#xff1a;如有下面表格&#xff0c;需要按笔试成绩整体排名。 解决步骤&#xff1a; 我们使用RANK函数即可实现单列整体排名。 Number 选择第一列。 Ref 选择这一整列&#xff08;CtrlShift向下箭头、再按F4&#xff09;。 "确定"即可计算…

C++ | Leetcode C++题解之第284题窥视迭代器

题目&#xff1a; 题解&#xff1a; template <class T> class PeekingIterator : public Iterator<T> { public:PeekingIterator(const vector<T>& nums) : Iterator<T>(nums) {flag Iterator<T>::hasNext();if (flag) {nextElement Ite…

AUTOSAR从入门到精通-CAN-FD

目录 几个高频面试题目 CAN与CAN FD的区别是什么? 一、CAN与CAN FD的概念 二、CAN与CANFD的比较 三、CAN与CANFD的优劣势 为何CANFD还不能大面积取代CAN总线? 算法原理 什么是CAN FD CAN FD的特点 为什么会出现CAN FD? CAN FD和CAN总线协议帧异同 Can FD报文讲解…

调用python-docx 提示出错

&#x1f3c6;本文收录于《CSDN问答解答》专栏&#xff0c;主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&收藏&…

【Django】 读取excel文件并在前端以网页形式显示-安装使用Pandas

文章目录 安装pandas写views写urls安装openpyxl重新调试 安装pandas Pandas是一个基于NumPy的Python数据分析库&#xff0c;可以从各种文件格式如CSV、JSON、SQL、Excel等导入数据&#xff0c;并支持多种数据运算操作&#xff0c;如归并、再成形、选择等。 更换pip源 pip co…