发布时间:2025-06-24 20:25:33 作者:北方职教升学中心 阅读量:196
Python魔法之旅
5、494-4、Python筑基之旅
2、pandas.DataFrame.diff方法pandas.DataFrame.diff(periods=1, axis=0)First discrete difference of element.Calculates the difference of a DataFrame element compared with another element in the DataFrame (default is element in previous row).Parameters:periodsint, default 1Periods to shift for calculating difference, accepts negative values.axis{0 or ‘index’, 1 or ‘columns’}, default 0Take difference over rows (0) or columns (1).Returns:DataFrameFirst differences of the Series.493-2、用法
492-6-1、pandas.DataFrame.eval方法import pandas as pd# 创建示例DataFramedata = { 'A': [1, 2, 3], 'B': [4, 5, 6],}df = pd.DataFrame(data)# 使用eval计算新列C = A + Bdf['C'] = df.eval('A + B')print("新列C:\n", df)# 或者在原地修改df.eval('D = A * B', inplace=True)print("\n新增列D,原地修改后:\n", df)# 使用额外的变量factor = 10df['E'] = df.eval('A + B + @factor')print("\n使用额外变量后的列E:\n", df)494-6-3、pandas.DataFrame.eval方法pandas.DataFrame.eval(expr, *, inplace=False, **kwargs)Evaluate a string describing operations on DataFrame columns.Operates on columns only, not specific rows or elements. This allows eval to run arbitrary code, which can make you vulnerable to code injection if you pass user input to this function.Parameters:exprstrThe expression string to evaluate.inplacebool, default FalseIf the expression contains an assignment, whether to perform the operation inplace and mutate the existing DataFrame. Otherwise, a new DataFrame is returned.**kwargsSee the documentation for eval() for complete details on the keyword arguments accepted by query().Returns:ndarray, scalar, pandas object, or NoneThe result of the evaluation or None if inplace=True.
494-2、标准差、
491-5-2、pandas.DataFrame.describe方法pandas.DataFrame.describe(percentiles=None, include=None, exclude=None)Generate descriptive statistics.Descriptive statistics include those that summarize the central tendency, dispersion and shape of a dataset’s distribution, excluding NaN values.Analyzes both numeric and object series, as well as DataFrame column sets of mixed data types. The output will vary depending on what is provided. Refer to the notes below for more detail.Parameters:percentileslist-like of numbers, optionalThe percentiles to include in the output. All should fall between 0 and 1. The default is [.25, .5, .75], which returns the 25th, 50th, and 75th percentiles.include‘all’, list-like of dtypes or None (default), optionalA white list of data types to include in the result. Ignored for Series. Here are the options:‘all’ : All columns of the input will be included in the output.A list-like of dtypes : Limits the results to the provided data types. To limit the result to numeric types submit numpy.number. To limit it instead to object columns submit the numpy.object data type. Strings can also be used in the style of select_dtypes (e.g. df.describe(include=['O'])). To select pandas categorical columns, use 'category'None (default) : The result will include all numeric columns.excludelist-like of dtypes or None (default), optional,A black list of data types to omit from the result. Ignored for Series. Here are the options:A list-like of dtypes : Excludes the provided data types from the result. To exclude numeric types submit numpy.number. To exclude object columns submit the data type numpy.object. Strings can also be used in the style of select_dtypes (e.g. df.describe(exclude=['O'])). To exclude pandas categorical columns, use 'category'None (default) : The result will exclude nothing.Returns:Series or DataFrameSummary statistics of the Series or Dataframe provided.492-2、
491-2-2、pandas.DataFrame.cumsum方法
491-1、缺失值:该方法会自动忽略缺失值(NaN)进行统计。skipna(可选,默认值为True):布尔值,是否忽略缺失值(NaNs),如果设置为False,将会计算缺失值。代码示例
# 491、用法精讲491、pandas.DataFrame.diff方法import pandas as pd# 创建一个示例DataFramedata = { 'A': [10, 20, 30, 25, 15], 'B': [100, 200, 300, 250, 150],}df = pd.DataFrame(data)# 计算差异(默认是前一行)diff_default = df.diff()print("默认差异:\n", diff_default)# 计算与前两行的差异diff_two_periods = df.diff(periods=2)print("\n与前两行的差异:\n", diff_two_periods)# 沿列方向计算差异diff_axis1 = df.diff(axis=1)print("\n沿列方向的差异:\n", diff_axis1)
493-6-3、pandas.DataFrame.describe方法492-1、用法
492-6-1、Python函数之旅3、代码示例
3、代码示例
493-6-3、inplace(可选,默认值为False):布尔值,如果为True,则在原DataFrame上进行修改,而不是返回一个新的DataFrame。减法、数据准备
无
493-6-2、495-3、include(可选,默认值为None):指定要包含的数据类型,可以是数据类型的字符串(如 'number', 'object', 'bool'),或者是数据类型的列表,如果该参数为None,将包含所有数据类型。
495-5-3、最小值、返回值 返回一个新的DataFrame(如果inplace=False)或原DataFrame(如果inplace=True),表达式计算的结果。percentiles
495-6-3、语法
493-2、数据类型:默认情况下,describe会根据数据类型自动选择要计算的列。Python筑基之旅
2、数据准备无
491-6-2、
493-3、
492-2-2、运算符支持:支持基本的算术运算符和一些常见的数学函数。自定义百分位数:可以根据需求自定义百分位数,以便更好地理解数据分布。axis(可选,默认值为None):{0 or 'index', 1 or 'columns'},指定沿哪个轴计算累积和,0表示按列计算,1表示按行计算。说明
495-5-1、*args(可选):其他的额外位置参数,通常在使用自定义函数时会用到。
492-4、说明
495-6、pandas.DataFrame.kurt方法495-1、适用数据
:该方法主要适用于数值型数据,对于其他类型的数据(如字符串)可能未按预期工作。
494-6、pandas.DataFrame.diff方法# 默认差异:# A B# 0 NaN NaN# 1 10.0 100.0# 2 10.0 100.0# 3 -5.0 -50.0# 4 -10.0 -100.0# # 与前两行的差异:# A B# 0 NaN NaN# 1 NaN NaN# 2 20.0 200.0# 3 5.0 50.0# 4 -15.0 -150.0# # 沿列方向的差异:# A B# 0 NaN 90# 1 NaN 180# 2 NaN 270# 3 NaN 225# 4 NaN 135494、
495-5-2、说明 491-5-1、功能 494-4、功能 492-4、效率:计算累积和的效率很高,适用于处理大规模数据集。491-5、
495-2-3、适用于时间序列:cumsum方法也常用于处理时间序列数据,以帮助识别随时间变化的总和趋势。
492-5-2、推荐阅读
1、
491-5-3、
494-5-3、说明
492-6、pandas.DataFrame.eval方法# 新列C:# A B C# 0 1 4 5# 1 2 5 7# 2 3 6 9# # 新增列D,原地修改后:# A B C D# 0 1 4 5 4# 1 2 5 7 10# 2 3 6 9 18# # 使用额外变量后的列E:# A B C D E# 0 1 4 5 4 15# 1 2 5 7 10 17# 2 3 6 9 18 19495、语法
494-2、乘法和除法。
493-5、用法
494-6-1、用法
493-6-1、pandas.DataFrame.describe方法# A B# count 5.000000 5.000000# mean 3.000000 30.000000# std 1.581139 15.811388# min 1.000000 10.000000# 25% 2.000000 20.000000# 50% 3.000000 30.000000# 75% 4.000000 40.000000# max 5.000000 50.000000# # A B# count 5.000000 5.000000# mean 3.000000 30.000000# std 1.581139 15.811388# min 1.000000 10.000000# 10% 1.400000 14.000000# 50% 3.000000 30.000000# 90% 4.600000 46.000000# max 5.000000 50.000000# # A B# count 5.000000 5.000000# mean 3.000000 30.000000# std 1.581139 15.811388# min 1.000000 10.000000# 25% 2.000000 20.000000# 50% 3.000000 30.000000# 75% 4.000000 40.000000# max 5.000000 50.000000# # C# count 5# unique 3# top a# freq 2493、推荐阅读
1、
495-5、代码示例
# 492、492-2-3、
493-6、数据准备
491-6-2、说明492-5-1、Python魔法之旅
5、用法
491-6-1、pandas.DataFrame.kurt方法# 每列的峭度:# A -4.3391# B NaN# C 0.0000# dtype: float64# 每行的峭度:# 0 NaN# 1 NaN# 2 NaN# 3 NaN# dtype: float64
二、
493-5-2、数据准备无
492-6-2、**kwargs
(可选):其他的额外关键字参数,通常在使用自定义函数时会用到。说明
494-5-1、参数
495-3、pandas.DataFrame.cumsum方法491-1、参数
491-3、
494-5、
495-2-2、结果输出# 494、用法
493-6-1、
493-2-2、代码示例# 494、结果输出
492、参数
492-3、博客个人主页
一、数据准备
494-6-2、pandas.DataFrame.cumsum方法# A B C# 0 1 4 7# 1 3 9 15# 2 6 15 24492、代码示例
# 495、
494-3、数据准备
无
494-6-2、参数
495-2-1、效率:eval方法在处理大型DataFrame时通常比逐列计算要快,尤其是在numexpr
可用的情况下。返回值
493-5、结果输出
二、参数
494-3、缺失值:如果数据中存在缺失值,根据skipna参数的设置,峭度可能会受到影响。
491-6、返回值
494-5、用法 返回一个与原DataFrame形状相同的新DataFrame,其中每个元素表示其前面所有元素的和。功能 用于生成DataFrame各列的描述性统计信息,它提供了数据的汇总,包括计数、NaN 值495-6-1、返回值
4、语法# 493、代码示例494-6-3、axis(可选,默认值为0):{0或'index', 1或'columns'},计算峭度的轴,0或'index'表示按列计算峭度,1或'columns'表示按行计算峭度。
495-2-4、返回值
495-5、语法# 494、
491-2-3、返回值 返回一个Series或DataFrame,包含每一列或每一行的峭度值。说明
494-6、用法精讲
491、
494-2-2、参数491-2-1、参数494-2-1、说明
491-6、语法# 491、返回值
491-5、结果输出
495、代码示例
492-6-3、均值、pandas.DataFrame.eval方法
494-1、说明493-5-1、**kwargs
(可选):额外的关键字参数,供底层函数使用。参数
493-3、功能 用于计算DataFrame中每列或每行的累积和,它返回一个与原DataFrame形状相同的新DataFrame,其中每个值是其前面所有值的和。数据准备
493-6-2、
491-2-4、博客个人主页
# 493、代码示例494-6-3、axis(可选,默认值为0):{0或'index', 1或'columns'},计算峭度的轴,0或'index'表示按列计算峭度,1或'columns'表示按行计算峭度。
495-2-4、返回值
495-5、语法# 494、
491-2-3、返回值 返回一个Series或DataFrame,包含每一列或每一行的峭度值。说明
494-6、用法精讲
491、
494-2-2、参数491-2-1、参数494-2-1、说明
491-6、语法# 491、返回值
491-5、结果输出
495、代码示例
492-6-3、均值、pandas.DataFrame.eval方法
494-1、说明493-5-1、**kwargs
(可选):额外的关键字参数,供底层函数使用。参数
493-3、功能 用于计算DataFrame中每列或每行的累积和,它返回一个与原DataFrame形状相同的新DataFrame,其中每个值是其前面所有值的和。数据准备
493-6-2、
491-2-4、博客个人主页