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

目录

一、用法精讲

451、pandas.DataFrame.pow方法

451-1、语法

451-2、参数

451-3、功能

451-4、返回值

451-5、说明

451-6、用法

451-6-1、数据准备

451-6-2、代码示例

451-6-3、结果输出

452、pandas.DataFrame.dot方法

452-1、语法

452-2、参数

452-3、功能

452-4、返回值

452-5、说明

452-6、用法

452-6-1、数据准备

452-6-2、代码示例

452-6-3、结果输出

453、pandas.DataFrame.radd方法

453-1、语法

453-2、参数

453-3、功能

453-4、返回值

453-5、说明

453-6、用法

453-6-1、数据准备

453-6-2、代码示例

453-6-3、结果输出

454、pandas.DataFrame.rsub方法

454-1、语法

454-2、参数

454-3、功能

454-4、返回值

454-5、说明

454-6、用法

454-6-1、数据准备

454-6-2、代码示例

454-6-3、结果输出

455、pandas.DataFrame.rmul方法

455-1、语法

455-2、参数

455-3、功能

455-4、返回值

455-5、说明

455-6、用法

455-6-1、数据准备

455-6-2、代码示例

455-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

451、pandas.DataFrame.pow方法
451-1、语法
# 456、pandas.DataFrame.pow方法
pandas.DataFrame.pow(other, axis='columns', level=None, fill_value=None)
Get Exponential power of dataframe and other, element-wise (binary operator pow).

Equivalent to dataframe ** other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rpow.

Among flexible wrappers (add, sub, mul, div, floordiv, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.

Parameters:
other
scalar, sequence, Series, dict or DataFrame
Any single or multiple element data structure, or list-like object.

axis
{0 or ‘index’, 1 or ‘columns’}
Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on.

level
int or label
Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value
float or None, default None
Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Returns:
DataFrame
Result of the arithmetic operation.
451-2、参数

451-2-1、other(必须)用于计算幂的对象,可以是另一个DataFrame、Series、标量或常数,DataFrame或Series应与调用的DataFrame形状兼容,以便进行元素级别的对齐。

451-2-2、axis(可选,默认值为'columns'){0 or ‘index’, 1 or ‘columns’}, 指定对齐的轴,如果是0或'index',则按行对齐;如果是1或'columns',则按列对齐,默认情况下按列对齐。

451-2-3、level(可选,默认值为None)如果使用MultiIndex(多级索引),可以指定在哪一层进行对齐,默认值为None,表示不使用多级索引。

451-2-4、fill_value(可选,默认值为None)在执行操作时,用于替换缺失值(NaN)的值,如果DataFrame中存在NaN值,操作之前将使用fill_value进行替换,默认值为None,即不替换NaN值。

451-3、功能

        用于逐元素地对DataFrame进行幂运算,该方法返回一个新的DataFrame,其中每个元素是调用DataFrame中的元素与other对应位置元素的幂。

451-4、返回值

        返回一个新的DataFrame,其中每个元素是调用DataFrame中元素与other对应位置元素的幂。

451-5、说明

        无

451-6、用法
451-6-1、数据准备
451-6-2、代码示例
# 456、pandas.DataFrame.pow方法
# 456-1、与标量的幂运算
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
result = df.pow(2)
print(result, end='\n\n')

# 456-2、与另一个DataFrame的幂运算
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
other = pd.DataFrame({
    'A': [2, 2, 2],
    'B': [3, 3, 3]
})
result = df.pow(other)
print(result, end='\n\n')

# 456-3、使用fill_value参数处理缺失值
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, None],
    'B': [4, None, 6]
})
df2 = pd.DataFrame({
    'A': [2, 2, 2],
    'B': [2, 2, 2]
})
result = df1.pow(df2, fill_value=1)
print(result)
451-6-3、结果输出
# 456、pandas.DataFrame.pow方法
# 456-1、与标量的幂运算
#    A   B
# 0  1  16
# 1  4  25
# 2  9  36

# 456-2、与另一个DataFrame的幂运算
#    A    B
# 0  1   64
# 1  4  125
# 2  9  216

# 456-3、使用fill_value参数处理缺失值
#      A     B
# 0  1.0  16.0
# 1  4.0   1.0
# 2  1.0  36.0
452、pandas.DataFrame.dot方法
452-1、语法
# 457、pandas.DataFrame.dot方法
pandas.DataFrame.dot(other)
Compute the matrix multiplication between the DataFrame and other.

This method computes the matrix product between the DataFrame and the values of an other Series, DataFrame or a numpy array.

It can also be called using self @ other.

Parameters:
other
Series, DataFrame or array-like
The other object to compute the matrix product with.

Returns:
Series or DataFrame
If other is a Series, return the matrix product between self and other as a Series. If other is a DataFrame or a numpy.array, return the matrix product of self and other in a DataFrame of a np.array.
452-2、参数

452-2-1、other(必须)要与之进行矩阵乘积的对象,可以是Series、DataFrame或numpy数组。

452-3、功能

        用于计算DataFrame与其他对象(如另一个DataFrame、Series或常量)之间的矩阵乘积,结果是一个新的DataFrame或Series,取决于操作数的类型和形状。

452-4、返回值

        返回的是矩阵乘积的结果,具体返回的类型和形状取决于输入的other参数的类型和形状。

452-5、说明

452-5-1、当与DataFrame进行点积时,列索引必须对齐(匹配)才能进行操作。

452-5-2、与Series进行点积时,Series的索引必须与DataFrame的列索引匹配。

452-5-3、与numpy数组进行点积时,数组的形状必须与DataFrame适当对齐(列数相同)。

452-6、用法
452-6-1、数据准备
452-6-2、代码示例
# 457、pandas.DataFrame.dot方法
# 457-1、DataFrame与DataFrame之间的点积
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
df2 = pd.DataFrame({
    'A': [7, 8],
    'B': [9, 10],
    'C': [11, 12]  # 注意:C列在这个操作中将被忽略,因为df1没有C列
})
# 假设我们只对A和B列感兴趣,并且想计算df1和df2中每对相应行的点积
# 我们需要先确保df1和df2的行数相同(这里不同,所以我们只处理共有的部分)
# 为了演示,我们只取df1的前两行与df2进行点积
common_rows = min(df1.shape[0], df2.shape[0])
# 使用numpy的点积功能,因为Pandas的dot()在这里不适用
import numpy as np
# 提取df1和df2的前两列(A和B)
df1_subset = df1[['A', 'B']].iloc[:common_rows]
df2_subset = df2[['A', 'B']]
# 计算点积
dot_products = np.dot(df1_subset.values, df2_subset.T.values)  # 注意转置df2_subset
print(dot_products, end='\n\n')

# 457-2、DataFrame与Series之间的点积
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
series = pd.Series([1, 2], index=['A', 'B'])
result = df1.dot(series)
print(result, end='\n\n')

# 457-3、DataFrame与array-like对象的点积
import numpy as np
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
array = np.array([[1, 2], [3, 4]])
result = df1.dot(array)
print(result)
452-6-3、结果输出
# 457、pandas.DataFrame.dot方法
# 457-1、DataFrame与DataFrame之间的点积
# [[43 48]
#  [59 66]]

# 457-2、DataFrame与Series之间的点积
# 0     9
# 1    12
# 2    15
# dtype: int64

# 457-3、DataFrame与array-like对象的点积
#     0   1
# 0  13  18
# 1  17  24
# 2  21  30
453、pandas.DataFrame.radd方法
453-1、语法
# 458、pandas.DataFrame.radd方法
pandas.DataFrame.radd(other, axis='columns', level=None, fill_value=None)
Get Addition of dataframe and other, element-wise (binary operator radd).

Equivalent to other + dataframe, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, add.

Among flexible wrappers (add, sub, mul, div, floordiv, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.

Parameters:
other
scalar, sequence, Series, dict or DataFrame
Any single or multiple element data structure, or list-like object.

axis
{0 or ‘index’, 1 or ‘columns’}
Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on.

level
int or label
Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value
float or None, default None
Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Returns:
DataFrame
Result of the arithmetic operation.
453-2、参数

453-2-1、other(必须)要加到DataFrame上的对象,可以是DataFrame、Series、标量等。

453-2-2、axis(可选,默认值为'columns'){0 or ‘index’, 1 or ‘columns’}, 指定对齐的轴,如果是0或'index',则按行对齐;如果是1或'columns',则按列对齐,默认情况下按列对齐。

453-2-3、level(可选,默认值为None)如果使用MultiIndex(多级索引),可以指定在哪一层进行对齐,默认值为None,表示不使用多级索引。

453-2-4、fill_value(可选,默认值为None)在执行操作时,用于替换缺失值(NaN)的值,如果DataFrame中存在NaN值,操作之前将使用fill_value进行替换,默认值为None,即不替换NaN值。

453-3、功能

        用于执行DataFrame(或者Series)与另一个对象(例如另一个DataFrame、Series、标量等)之间的反向加法运算,反向加法运算指的是执行other + DataFrame而不是DataFrame + other,该方法通常在实现对象的反向运算符重载时被调用,例如在其他对象调用加法运算时,DataFrame作为右操作数。

453-4、返回值

        返回一个新的DataFrame,其中包含了原DataFrame与other进行加法运算后的结果,返回的DataFrame的形状和索引根据参与运算的两个对象自动对齐,缺失值(NaN)根据fill_value参数处理。具体详情如下:

  • 如果other是标量,则对DataFrame中的每个元素都加上该标量。
  • 如果other是另一个DataFrame或Series,则对应位置的元素相加,索引不匹配的地方会产生NaN,除非使用fill_value参数来指定替代值。
  • 如果存在多级索引,可以通过指定level参数来在特定级别上对齐和计算。
453-5、说明

        无

453-6、用法
453-6-1、数据准备
453-6-2、代码示例
# 458、pandas.DataFrame.radd方法
# 458-1、使用标量进行反向加法运算
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
result_scalar = df.radd(10)
print(result_scalar, end='\n\n')

# 458-2、使用另一个DataFrame进行反向加法运算
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
# 创建另一个DataFrame
df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result_df = df.radd(df2)
print(result_df, end='\n\n')

# 458-3、使用fill_value进行反向加法运算
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
df3 = pd.DataFrame({
    'A': [10, 20],
    'C': [70, 80]
})
result_fill_value = df.radd(df3, fill_value=0)
print(result_fill_value)
453-6-3、结果输出
# 458、pandas.DataFrame.radd方法
# 458-1、使用标量进行反向加法运算
#     A   B
# 0  11  14
# 1  12  15
# 2  13  16

# 458-2、使用另一个DataFrame进行反向加法运算
#     A   B
# 0  11  44
# 1  22  55
# 2  33  66

# 458-3、使用fill_value进行反向加法运算
#       A    B     C
# 0  11.0  4.0  70.0
# 1  22.0  5.0  80.0
# 2   3.0  6.0   NaN
454、pandas.DataFrame.rsub方法
454-1、语法
# 459、pandas.DataFrame.rsub方法
pandas.DataFrame.rsub(other, axis='columns', level=None, fill_value=None)
Get Subtraction of dataframe and other, element-wise (binary operator rsub).

Equivalent to other - dataframe, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, sub.

Among flexible wrappers (add, sub, mul, div, floordiv, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.

Parameters:
other
scalar, sequence, Series, dict or DataFrame
Any single or multiple element data structure, or list-like object.

axis
{0 or ‘index’, 1 or ‘columns’}
Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on.

level
int or label
Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value
float or None, default None
Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Returns:
DataFrame
Result of the arithmetic operation.
454-2、参数

454-2-1、other(必须)要减去的对象,可以是一个数值、Series、DataFrame或一个具有兼容形状的数组。如果other是一个常量(比如数字5),那么这个常量将被用作所有元素的被减数;如果是一个DataFrame或Series,则进行逐元素操作。

454-2-2、axis(可选,默认值为'columns'){0 or ‘index’, 1 or ‘columns’}, 指定对齐的轴,如果是0或'index',则按行对齐;如果是1或'columns',则按列对齐,默认情况下按列对齐。

454-2-3、level(可选,默认值为None)如果使用MultiIndex(多级索引),可以指定在哪一层进行对齐,默认值为None,表示不使用多级索引。

454-2-4、fill_value(可选,默认值为None)在执行操作时,用于替换缺失值(NaN)的值,如果DataFrame中存在NaN值,操作之前将使用fill_value进行替换,默认值为None,即不替换NaN值。

454-3、功能

        执行反向的减法运算。例如,如果有两个数据帧df1和df2,那么df1.rsub(df2)将计算df2-df1。如果df1和df2形状相同,减法将在相应位置逐元素执行;如果df2是一个标量值(如整数或浮点数),那么此标量将用作被减数,数据帧df1中的每个元素作为减数。

454-4、返回值

        返回一个新的DataFrame或Series,包含反向减法操作的结果,原始数据帧df1不会被修改,rsub会生成一个新的数据结构,返回的数据结构的形状和df1相同,并且其元素是根据指定的减法操作计算得出的。

454-5、说明

        无

454-6、用法
454-6-1、数据准备
454-6-2、代码示例
# 459、pandas.DataFrame.rsub方法
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [10, 10, 10], 'B': [10, 10, 10]})
result = df1.rsub(df2)
print(result)
454-6-3、结果输出
# 459、pandas.DataFrame.rsub方法
#    A  B
# 0  9  6
# 1  8  5
# 2  7  4
455、pandas.DataFrame.rmul方法
455-1、语法
# 460、pandas.DataFrame.rmul方法
pandas.DataFrame.rmul(other, axis='columns', level=None, fill_value=None)
Get Multiplication of dataframe and other, element-wise (binary operator rmul).

Equivalent to other * dataframe, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, mul.

Among flexible wrappers (add, sub, mul, div, floordiv, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.

Parameters:
other
scalar, sequence, Series, dict or DataFrame
Any single or multiple element data structure, or list-like object.

axis
{0 or ‘index’, 1 or ‘columns’}
Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on.

level
int or label
Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value
float or None, default None
Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Returns:
DataFrame
Result of the arithmetic operation.
455-2、参数

455-2-1、other(必须)要乘的对象,可以是一个数值、Series、DataFrame或一个具有兼容形状的数组。如果other是一个常量,那么这个常量将被用作所有元素的乘数;如果是一个DataFrame或Series,则进行逐元素操作。

455-2-2、axis(可选,默认值为'columns'){0 or ‘index’, 1 or ‘columns’}, 指定对齐的轴,如果是0或'index',则按行对齐;如果是1或'columns',则按列对齐,默认情况下按列对齐。

455-2-3、level(可选,默认值为None)如果使用MultiIndex(多级索引),可以指定在哪一层进行对齐,默认值为None,表示不使用多级索引。

455-2-4、fill_value(可选,默认值为None)在执行操作时,用于替换缺失值(NaN)的值,如果DataFrame中存在NaN值,操作之前将使用fill_value进行替换,默认值为None,即不替换NaN值。

455-3、功能

        执行反向的乘法运算。例如,如果有两个数据帧df1和df2,那么df1.rmul(df2)将计算df2*df1。如果df1和df2形状相同,乘法将在相应位置逐元素执行df1和df2;如果df2是一个标量值(如整数或浮点数),那么此标量将用作乘数,数据帧df1中的每个元素作为被乘数。

455-4、返回值

        返回一个新的DataFrame或Series,包含反向乘法操作的结果,原始数据帧df1不会被修改,rmul会生成一个新的数据结构,返回的数据结构的形状和df1相同,并且其元素是根据指定的乘法操作计算得出的。

455-5、说明

        无

455-6、用法
455-6-1、数据准备
455-6-2、代码示例
# 460、pandas.DataFrame.rmul方法
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [10, 10, 10], 'B': [10, 10, 10]})
result = df1.rmul(df2)
print(result)
455-6-3、结果输出
# 460、pandas.DataFrame.rmul方法
#     A   B
# 0  10  40
# 1  20  50
# 2  30  60

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇夜光杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值