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

2024-08-31 12:28

本文主要是介绍Python酷库之旅-第三方库Pandas(111),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、用法精讲

486、pandas.DataFrame.count方法

486-1、语法

486-2、参数

486-3、功能

486-4、返回值

486-5、说明

486-6、用法

486-6-1、数据准备

486-6-2、代码示例

486-6-3、结果输出

487、pandas.DataFrame.cov方法

487-1、语法

487-2、参数

487-3、功能

487-4、返回值

487-5、说明

487-6、用法

487-6-1、数据准备

487-6-2、代码示例

487-6-3、结果输出

488、pandas.DataFrame.cummax方法

488-1、语法

488-2、参数

488-3、功能

488-4、返回值

488-5、说明

488-6、用法

488-6-1、数据准备

488-6-2、代码示例

488-6-3、结果输出

489、pandas.DataFrame.cummin方法

489-1、语法

489-2、参数

489-3、功能

489-4、返回值

489-5、说明

489-6、用法

489-6-1、数据准备

489-6-2、代码示例

489-6-3、结果输出

490、pandas.DataFrame.cumprod方法

490-1、语法

490-2、参数

490-3、功能

490-4、返回值

490-5、说明

490-6、用法

490-6-1、数据准备

490-6-2、代码示例

490-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

486、pandas.DataFrame.count方法
486-1、语法
# 486、pandas.DataFrame.count方法
pandas.DataFrame.count(axis=0, numeric_only=False)
Count non-NA cells for each column or row.The values None, NaN, NaT, pandas.NA are considered NA.Parameters:
axis
{0 or ‘index’, 1 or ‘columns’}, default 0
If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.numeric_only
bool, default False
Include only float, int or boolean data.Returns:
Series
For each column/row the number of non-NA/null entries.
486-2、参数

486-2-1、axis(可选,默认值为0){0或'index', 1或'columns'},0或'index',沿着行的方向计算(即为每一列计数);1或'columns',沿着列的方向计算(即为每一行计数)。

486-2-2、numeric_only(可选,默认值为False)布尔值,如果为True,则只会计算数值型数据的数量,对于非数值型数据将返回NaN。

486-3、功能

        用于计算DataFrame中每一列或每一行的非NA/null值的数量,默认情况下,它会计算每一列的非缺失值数量,也可以通过指定参数来计算每一行的数量。

486-4、返回值

        返回一个Series,其中包含每列或每行的非缺失值的数量。

486-5、说明

        无

486-6、用法
486-6-1、数据准备
486-6-2、代码示例
# 486、pandas.DataFrame.count方法
import pandas as pd
import numpy as np
# 创建一个示例DataFrame
data = {'A': [1, 2, np.nan, 4],'B': [np.nan, np.nan, np.nan, 1],'C': [1, 2, 3, 4]
}
df = pd.DataFrame(data)
# 计算每列的非NA数量
column_count = df.count()
print(column_count)
# 计算每行的非NA数量
row_count = df.count(axis=1)
print(row_count)
486-6-3、结果输出
# 486、pandas.DataFrame.count方法
# A    3
# B    1
# C    4
# dtype: int64
# 0    2
# 1    2
# 2    1
# 3    3
# dtype: int64
487、pandas.DataFrame.cov方法
487-1、语法
# 487、pandas.DataFrame.cov方法
pandas.DataFrame.cov(min_periods=None, ddof=1, numeric_only=False)
Compute pairwise covariance of columns, excluding NA/null values.Compute the pairwise covariance among the series of a DataFrame. The returned data frame is the covariance matrix of the columns of the DataFrame.Both NA and null values are automatically excluded from the calculation. (See the note below about bias from missing values.) A threshold can be set for the minimum number of observations for each value created. Comparisons with observations below this threshold will be returned as NaN.This method is generally used for the analysis of time series data to understand the relationship between different measures across time.Parameters:
min_periodsint, optional
Minimum number of observations required per pair of columns to have a valid result.ddofint, default 1
Delta degrees of freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. This argument is applicable only when no nan is in the dataframe.numeric_onlybool, default False
Include only float, int or boolean data.New in version 1.5.0.Changed in version 2.0.0: The default value of numeric_only is now False.Returns:
DataFrame
The covariance matrix of the series of the DataFrame.
487-2、参数

487-2-1、min_periods(可选,默认值为None)整数,在计算协方差时,最小的观测值数量,只有在有效观测值数量大于或等于min_periods的情况下,才会计算协方差。

487-2-2、ddof(可选,默认值为1)整数,自由度的调整,计算样本协方差时,通常设为1(这是样本协方差的默认计算方式),如果设置为0,则计算总体协方差。

487-2-3、numeric_only(可选,默认值为False)布尔值,如果为True,则只考虑数值型数据的列进行协方差计算。

487-3、功能

        用于计算DataFrame中各列之间的协方差矩阵,协方差矩阵是一个方阵,用于表示每对变量之间的协方差,能够帮助分析变量之间的关系。

487-4、返回值

        返回一个协方差矩阵(DataFrame):

  • 行和列分别对应于DataFrame的列。
  • 矩阵中的每个值表示对应列之间的协方差。
487-5、说明

487-5-1、N/A处理:cov方法会自动忽略缺失值(NA)的行。

487-5-2、数值型数据:仅计算数值型列之间的协方差,非数值型列会被排除(如果numeric_only=True)。

487-5-3、自由度:ddof参数允许用户控制协方差的计算方式,常用于样本和总体之间的选择。

487-6、用法
487-6-1、数据准备
487-6-2、代码示例
# 487、pandas.DataFrame.cov方法
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [1, 2, 3, 4],'B': [4, 5, 6, 7],'C': [7, 8, 9, 10]
}
df = pd.DataFrame(data)
# 计算协方差矩阵
cov_matrix = df.cov()
print(cov_matrix)
487-6-3、结果输出
# 487、pandas.DataFrame.cov方法
#           A         B         C
# A  1.666667  1.666667  1.666667
# B  1.666667  1.666667  1.666667
# C  1.666667  1.666667  1.666667
488、pandas.DataFrame.cummax方法
488-1、语法
# 488、pandas.DataFrame.cummax方法
pandas.DataFrame.cummax(axis=None, skipna=True, *args, **kwargs)
Return cumulative maximum over a DataFrame or Series axis.Returns a DataFrame or Series of the same size containing the cumulative maximum.Parameters:
axis
{0 or ‘index’, 1 or ‘columns’}, default 0
The index or the name of the axis. 0 is equivalent to None or ‘index’. For Series this parameter is unused and defaults to 0.skipna
bool, default True
Exclude NA/null values. If an entire row/column is NA, the result will be NA.*args, **kwargs
Additional keywords have no effect but might be accepted for compatibility with NumPy.Returns:
Series or DataFrame
Return cumulative maximum of Series or DataFrame.
488-2、参数

488-2-1、axis(可选,默认值为None){0 or 'index', 1 or 'columns'},指定沿哪个轴计算累积最大值,0表示按列计算,1表示按行计算。

488-2-2、skipna(可选,默认值为True)布尔值,是否忽略缺失值(NaN),如果为True,缺失值将被忽略;如果为False,结果中可能会出现NaN。

488-2-3、*args(可选)其他的额外位置参数,通常在使用自定义函数时会用到。

488-2-4、**kwargs(可选)其他的额外关键字参数,通常在使用自定义函数时会用到。

488-3、功能

        用于计算DataFrame中每列或每行的累积最大值,它返回一个与原DataFrame形状相同的新DataFrame,其中每个值都是其前面所有值的最大值。

488-4、返回值

        返回一个与原DataFrame形状相同的新DataFrame,其中每个元素表示其前面所有元素的最大值。

488-5、说明

488-5-1、缺失值处理:如果skinpa为True,所有NaN值将被忽略;如果为False,结果中的任何NaN都会导致后续的结果也为NaN。

488-5-2、可用于时间序列:cummax方法也可以用于处理时间序列数据,有助于分析随时间推移的最大值变化。

488-5-3、效率:计算累积最大值的效率高,适用于大规模数据集

488-6、用法
488-6-1、数据准备
488-6-2、代码示例
# 488、pandas.DataFrame.cummax方法
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [1, 2, 3, 4],'B': [4, 3, 2, 1],'C': [1, 5, 2, 3]
}
df = pd.DataFrame(data)
# 计算累积最大值
cummax_df = df.cummax()
print(cummax_df)
488-6-3、结果输出
# 488、pandas.DataFrame.cummax方法
#    A  B  C
# 0  1  4  1
# 1  2  4  5
# 2  3  4  5
# 3  4  4  5
489、pandas.DataFrame.cummin方法
489-1、语法
# 489、pandas.DataFrame.cummin方法
pandas.DataFrame.cummin(axis=None, skipna=True, *args, **kwargs)
Return cumulative minimum over a DataFrame or Series axis.Returns a DataFrame or Series of the same size containing the cumulative minimum.Parameters:
axis
{0 or ‘index’, 1 or ‘columns’}, default 0
The index or the name of the axis. 0 is equivalent to None or ‘index’. For Series this parameter is unused and defaults to 0.skipna
bool, default True
Exclude NA/null values. If an entire row/column is NA, the result will be NA.*args, **kwargs
Additional keywords have no effect but might be accepted for compatibility with NumPy.Returns:
Series or DataFrame
Return cumulative minimum of Series or DataFrame.
489-2、参数

489-2-1、axis(可选,默认值为None){0 or 'index', 1 or 'columns'},指定沿哪个轴计算累积最小值,0表示按列计算,1表示按行计算。

489-2-2、skipna(可选,默认值为True)布尔值,是否忽略缺失值(NaN),如果为True,缺失值将被忽略;如果为False,结果中可能会出现NaN。

489-2-3、*args(可选)其他的额外位置参数,通常在使用自定义函数时会用到。

489-2-4、**kwargs(可选)其他的额外关键字参数,通常在使用自定义函数时会用到。

489-3、功能

        用于计算 DataFrame 中每列或每行的累积最小值,它返回一个与原 DataFrame 形状相同的新 DataFrame,其中每个值都是其前面所有值的最小值。

489-4、返回值

        返回一个与原 DataFrame 形状相同的新 DataFrame,其中每个元素表示其前面所有元素的最小值。

489-5、说明

489-5-1、缺失值处理:如果skipna为True,所有NaN值将被忽略;如果为False,结果中的任何NaN都会导致后续的结果也为NaN。

489-5-2、适用于时间序列:cummin方法也可以用于处理时间序列数据,有助于分析随时间推移的最小值变化。

489-5-3、效率:计算累积最小值的效率高,适用于大规模数据集。

489-6、用法
489-6-1、数据准备
489-6-2、代码示例
# 489、pandas.DataFrame.cummin方法
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [5, 3, 6, 2],'B': [1, 2, 4, 0],'C': [3, 7, 1, 5]
}
df = pd.DataFrame(data)
# 计算累积最小值
cummin_df = df.cummin()
print(cummin_df)
489-6-3、结果输出
# 489、pandas.DataFrame.cummin方法
#    A  B  C
# 0  5  1  3
# 1  3  1  3
# 2  3  1  1
# 3  2  0  1
490、pandas.DataFrame.cumprod方法
490-1、语法
# 490、pandas.DataFrame.cumprod方法
pandas.DataFrame.cumprod(axis=None, skipna=True, *args, **kwargs)
Return cumulative product over a DataFrame or Series axis.Returns a DataFrame or Series of the same size containing the cumulative product.Parameters:
axis
{0 or ‘index’, 1 or ‘columns’}, default 0
The index or the name of the axis. 0 is equivalent to None or ‘index’. For Series this parameter is unused and defaults to 0.skipna
bool, default True
Exclude NA/null values. If an entire row/column is NA, the result will be NA.*args, **kwargs
Additional keywords have no effect but might be accepted for compatibility with NumPy.Returns:
Series or DataFrame
Return cumulative product of Series or DataFrame.
490-2、参数

490-2-1、axis(可选,默认值为None){0 or 'index', 1 or 'columns'},指定沿哪个轴计算累积乘积,0表示按列计算,1表示按行计算。

490-2-2、skipna(可选,默认值为True)布尔值,是否忽略缺失值(NaN),如果为True,缺失值将被忽略;如果为False,结果中可能会出现NaN。

490-2-3、*args(可选)其他的额外位置参数,通常在使用自定义函数时会用到。

490-2-4、**kwargs(可选)其他的额外关键字参数,通常在使用自定义函数时会用到。

490-3、功能

        用于计算DataFrame中每列或每行的累积乘积,它返回一个与原DataFrame形状相同的新DataFrame,其中每个值都是其前面所有值的乘积。

490-4、返回值

        返回一个与原DataFrame形状相同的新DataFrame,其中每个元素表示其前面所有元素的乘积。

490-5、说明

490-5-1、缺失值处理:如果skipna为True,所有NaN值将被忽略;如果为False,结果中的任何NaN都会导致后续的结果也为NaN。

490-5-2、适用于时间序列:cumprod方法也可以用于处理时间序列数据,有助于分析随时间推移的乘积变化。

490-5-3、效率: 计算累积乘积的效率高,适用于大规模数据集。

490-6、用法
490-6-1、数据准备
490-6-2、代码示例
# 490、pandas.DataFrame.cumprod方法
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [2, 3, 4],'B': [1, 2, 3],'C': [5, 6, 7]
}
df = pd.DataFrame(data)
# 计算累积乘积
cumprod_df = df.cumprod()
print(cumprod_df)
490-6-3、结果输出
# 490、pandas.DataFrame.cumprod方法
#     A  B    C
# 0   2  1    5
# 1   6  2   30
# 2  24  6  210

二、推荐阅读

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

这篇关于Python酷库之旅-第三方库Pandas(111)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123902

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数