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

CSDN 2024-09-03 09:05:16 阅读 97

目录

一、用法精讲

481、pandas.DataFrame.all方法

481-1、语法

481-2、参数

481-3、功能

481-4、返回值

481-5、说明

481-6、用法

481-6-1、数据准备

481-6-2、代码示例

481-6-3、结果输出

482、pandas.DataFrame.any方法

482-1、语法

482-2、参数

482-3、功能

482-4、返回值

482-5、说明

482-6、用法

482-6-1、数据准备

482-6-2、代码示例

482-6-3、结果输出

483、pandas.DataFrame.clip方法

483-1、语法

483-2、参数

483-3、功能

483-4、返回值

483-5、说明

483-6、用法

483-6-1、数据准备

483-6-2、代码示例

483-6-3、结果输出

484、pandas.DataFrame.corr方法

484-1、语法

484-2、参数

484-3、功能

484-4、返回值

484-5、说明

484-6、用法

484-6-1、数据准备

484-6-2、代码示例

484-6-3、结果输出

485、pandas.DataFrame.corrwith方法

485-1、语法

485-2、参数

485-3、功能

485-4、返回值

485-5、说明

485-6、用法

485-6-1、数据准备

485-6-2、代码示例

485-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

481、pandas.DataFrame.all方法
481-1、语法

<code># 481、pandas.DataFrame.all方法

pandas.DataFrame.all(axis=0, bool_only=False, skipna=True, **kwargs)

Return whether all elements are True, potentially over an axis.

Returns True unless there at least one element within a series or along a Dataframe axis that is False or equivalent (e.g. zero or 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 True, 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:

Series or DataFrame

If level is specified, then, DataFrame is returned; otherwise, Series is returned.

481-2、参数

481-2-1、axis(可选,默认值为0){0或'index', 1或'columns'},指定沿着哪个轴进行计算,0或'index'表示对每一列进行操作,结果是每一列的布尔值;1或'columns'表示对每一行进行操作,结果是每一行的布尔值。

481-2-2、bool_only(可选,默认值为False)布尔值,如果为True,则只考虑布尔类型的数据,如果DataFrame中包含其他类型的数据,则这些列将被忽略。

481-2-3、skipna(可选,默认值为True)布尔值,如果为True,则在计算时会忽略NaN值;如果为False,则如果存在NaN值,结果将为False。

481-2-4、**kwargs(可选)用于传递给底层函数的其他关键字参数。

481-3、功能

        用于检查DataFrame在指定轴上是否所有元素都为真值,它通常用于数据清洗、验证等场景,例如检查某一列或某一行是否满足特定条件。

481-4、返回值

        返回一个布尔值(如果axis=0)或布尔Series(如果axis=1),表示指定轴上的所有元素是否都为真。如果axis=0,返回的Series中每个元素对应于DataFrame每一列的结果;如果axis=1,返回的Series中每个元素对应于DataFrame每一行的结果。

481-5、说明

        无

481-6、用法
481-6-1、数据准备

481-6-2、代码示例

# 481、pandas.DataFrame.all方法

# 481-1、检查每一列是否所有元素都为True

import pandas as pd

# 创建一个示例DataFrame

data = {

'A': [True, True, False],

'B': [True, True, True],

'C': [False, True, True]

}

df = pd.DataFrame(data)

result_col = df.all(axis=0)

print(result_col)

# 481-2、检查每一行是否所有元素都为True

import pandas as pd

# 创建一个示例DataFrame

data = {

'A': [True, True, False],

'B': [True, True, True],

'C': [False, True, True]

}

df = pd.DataFrame(data)

result_row = df.all(axis=1)

print(result_row)

481-6-3、结果输出

# 481、pandas.DataFrame.all方法

# 481-1、检查每一列是否所有元素都为True

# A False

# B True

# C False

# dtype: bool

# 481-2、检查每一行是否所有元素都为True

# 0 False

# 1 True

# 2 False

# dtype: bool

482、pandas.DataFrame.any方法
482-1、语法

# 482、pandas.DataFrame.any方法

pandas.DataFrame.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:

Series or DataFrame

If level is specified, then, DataFrame is returned; otherwise, Series is returned.

482-2、参数

482-2-1、axis(可选,默认值为0){0或'index', 1或'columns'},指定沿着哪个轴进行操作,0或'index'表示沿着行进行检查,1或'columns'表示沿着列进行检查。

482-2-2、bool_only(可选,默认值为False)布尔值,如果为True,则仅考虑布尔列;如果为False,则考虑所有列,即使它们不是布尔类型。

482-2-3、skipna(可选,默认值为True)布尔值,是否忽略NaN值,若设为True,NaN不会影响结果;若设为False,一旦遇到NaN,将返回False。

482-2-4、**kwargs(可选)额外的关键字参数,通常用于进一步控制输出或行为,但在大多数情况下不常用。

482-3、功能

        用于检查DataFrame是否有任何一个元素为True,可以用来快速判断某一列或某一行是否包含有效的(非零、非空等)数据。

482-4、返回值

        如果axis=0,返回一个包含每一列是否有任何元素为True的布尔值的Series;如果axis=1,则返回一个包含每一行是否有任何元素为True的布尔值的Series。如果DataFrame是空的,返回的Series将是空的。

482-5、说明

        无

482-6、用法
482-6-1、数据准备

482-6-2、代码示例

# 482、pandas.DataFrame.any方法

import pandas as pd

# 创建示例DataFrame

data = {

'A': [False, False, True],

'B': [False, False, False],

'C': [True, False, True]

}

df = pd.DataFrame(data)

# 沿着列检查

print(df.any(axis=0))

# 沿着行检查

print(df.any(axis=1))

482-6-3、结果输出

# 482、pandas.DataFrame.any方法

# A True

# B False

# C True

# dtype: bool

# 0 True

# 1 False

# 2 True

# dtype: bool

483、pandas.DataFrame.clip方法
483-1、语法

# 483、pandas.DataFrame.clip方法

pandas.DataFrame.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.

483-2、参数

483-2-1、lower(可选,默认值为None)标量、数组、Series或DataFrame,指定元素的下界,如果元素小于此值,则将其替换为此值。

483-2-2、upper(可选,默认值为None)标量、数组、Series或DataFrame,指定元素的上界,如果元素大于此值,则将其替换为此值。

483-2-3、axis(可选,默认值为None){0或'index', 1或'columns'},用于对齐输入的lower和upper参数,如果没有指定,方法将对DataFrame的所有元素进行操作。

483-2-4、inplace(可选,默认值为False)布尔值,如果为True,则直接在原DataFrame上进行操作;如果为False,则返回一个新的DataFrame,而不修改原有DataFrame。

483-2-5、**kwargs(可选)额外的关键字参数,通常不常用。

483-3、功能

        用于将DataFrame中的值限制在给定的下界和上界之间,超出范围的值会被替换为下界或上界,这在数据预处理和清洗时非常有用。

483-4、返回值

        返回一个新的DataFrame,具有与原始DataFrame相同的索引和列,但所有值都被限制在指定的上下界范围内(如果存在)。

483-5、说明

        无

483-6、用法
483-6-1、数据准备

483-6-2、代码示例

# 483、pandas.DataFrame.clip方法

import pandas as pd

# 创建示例DataFrame

data = {

'A': [1, 2, 3, 4],

'B': [5, 6, 7, 8],

'C': [9, 10, 11, 12]

}

df = pd.DataFrame(data)

# 使用clip方法

clipped_df = df.clip(lower=3, upper=8)

print(clipped_df)

483-6-3、结果输出

# 483、pandas.DataFrame.clip方法

# A B C

# 0 3 5 8

# 1 3 6 8

# 2 3 7 8

# 3 4 8 8

484、pandas.DataFrame.corr方法
484-1、语法

# 484、pandas.DataFrame.corr方法

pandas.DataFrame.corr(method='pearson', min_periods=1, numeric_only=False)code>

Compute pairwise correlation of columns, excluding NA/null values.

Parameters:

method{‘pearson’, ‘kendall’, ‘spearman’} or callable

Method of 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. 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 required per pair of columns to have a valid result. Currently only available for Pearson and Spearman correlation.

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

Correlation matrix.

484-2、参数

484-2-1、method(可选,默认值为'pearson')字符串,可以选择以下几种方法计算相关系数:

'pearson':皮尔逊相关系数(默认选项)'kendall':肯德尔相关系数'spearman':斯皮尔曼等级相关系数

484-2-2、min_periods(可选,默认值为1)整数,表示在计算相关系数时所需的最小观测值数量,只有当两个列都有至少min_periods次有效值时,才会计算它们的相关系数。

484-2-3、numeric_only(可选,默认值为False)布尔值,表示是否只计算数字列的相关性,如果设置为True,将忽略非数字列。

484-3、功能

        用于计算数据框中各列之间的相关系数,相关系数是衡量变量之间线性关系强度的统计量。

484-4、返回值

        返回一个DataFrame,其中包含输入DataFrame中各列之间的相关系数,行和列的索引为原DataFrame的列名。

484-5、说明

484-5-1、相关系数的值范围从-1到1,-1表示完全负相关,1表示完全正相关,0表示没有线性相关关系。

484-5-2、在数据缺失的情况下,通过min_periods可以控制是否计算相关系数。

484-6、用法
484-6-1、数据准备

484-6-2、代码示例

# 484、pandas.DataFrame.corr方法

import pandas as pd

# 创建一个示例DataFrame

data = {

'A': [1, 2, 3, 4, 5],

'B': [5, 4, 3, 2, 1],

'C': [1, 2, None, 4, 5]

}

df = pd.DataFrame(data)

# 计算相关系数矩阵

correlation_matrix = df.corr(method='pearson', min_periods=1)code>

print(correlation_matrix)

484-6-3、结果输出

# 484、pandas.DataFrame.corr方法

# A B C

# A 1.0 -1.0 1.0

# B -1.0 1.0 -1.0

# C 1.0 -1.0 1.0

485、pandas.DataFrame.corrwith方法
485-1、语法

# 485、pandas.DataFrame.corrwith方法

pandas.DataFrame.corrwith(other, axis=0, drop=False, method='pearson', numeric_only=False)code>

Compute pairwise correlation.

Pairwise correlation is computed between rows or columns of DataFrame with rows or columns of Series or DataFrame. DataFrames are first aligned along both axes before computing the correlations.

Parameters:

otherDataFrame, Series

Object with which to compute correlations.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to use. 0 or ‘index’ to compute row-wise, 1 or ‘columns’ for column-wise.

dropbool, default False

Drop missing indices from result.

method{‘pearson’, ‘kendall’, ‘spearman’} or callable

Method of 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.

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:

Series

Pairwise correlations.

485-2、参数

485-2-1、other(必须)要与之计算相关性的另一个DataFrame或Series,它的数据结构应该可以进行对齐,通常是同一列或行标签。

485-2-2、axis(可选,默认值为0){0或'index', 1或'columns'},0或'index'表示按列计算相关性(默认值);1或'columns'表示按行计算相关性,将other的行与当前DataFrame的列进行比较。

485-2-3、drop(可选,默认值为False)布尔值,指示是否在计算之前丢弃包含缺失值的行,如果设为True,则将不考虑那些含有NA值的行。

485-2-4、method(可选,默认值为'pearson')字符串,可以选择以下几种方法计算相关系数:

'pearson':皮尔逊相关系数(默认选项)'kendall':肯德尔相关系数'spearman':斯皮尔曼等级相关系数

485-2-5、numeric_only(可选,默认值为False)布尔值,若设置为True,则仅考虑数值类型的列进行计算,非数值列将被排除。

485-3、功能

        用于计算一个DataFrame与另一个DataFrame或Series之间的相关性。

485-4、返回值

        返回的是一个包含相关系数的Series,索引为原DataFrame或Series的列或索引名,值为相关系数。

485-5、说明

        无

485-6、用法
485-6-1、数据准备

485-6-2、代码示例

# 485、pandas.DataFrame.corrwith方法

import pandas as pd

# 创建示例DataFrame

df1 = pd.DataFrame({

'A': [1, 2, 3, 4, 5],

'B': [5, 4, 3, 2, 1]

})

df2 = pd.DataFrame({

'A': [1, 3, 5, 7, 9],

'B': [9, 7, 5, 3, 1]

})

# 计算df1与df2的相关性

correlation = df1.corrwith(df2, axis=0, drop=False, method='pearson', numeric_only=False)code>

print(correlation)

485-6-3、结果输出

# 485、pandas.DataFrame.corrwith方法

# A 1.0

# B 1.0

# dtype: float64

二、推荐阅读

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


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。