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的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e