当前位置: 首页 > article >正文

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

目录

一、用法精讲

446、pandas.DataFrame.mul方法

446-1、语法

446-2、参数

446-3、功能

446-4、返回值

446-5、说明

446-6、用法

446-6-1、数据准备

446-6-2、代码示例

446-6-3、结果输出

447、pandas.DataFrame.div方法

447-1、语法

447-2、参数

447-3、功能

447-4、返回值

447-5、说明

447-6、用法

447-6-1、数据准备

447-6-2、代码示例

447-6-3、结果输出

448、pandas.DataFrame.truediv方法

448-1、语法

448-2、参数

448-3、功能

448-4、返回值

448-5、说明

448-6、用法

448-6-1、数据准备

448-6-2、代码示例

448-6-3、结果输出

449、pandas.DataFrame.floordiv方法

449-1、语法

449-2、参数

449-3、功能

449-4、返回值

449-5、说明

449-6、用法

449-6-1、数据准备

449-6-2、代码示例

449-6-3、结果输出

450、pandas.DataFrame.mod方法

450-1、语法

450-2、参数

450-3、功能

450-4、返回值

450-5、说明

450-6、用法

450-6-1、数据准备

450-6-2、代码示例

450-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

446、pandas.DataFrame.mul方法
446-1、语法
# 446、pandas.DataFrame.mul方法
pandas.DataFrame.mul(other, axis='columns', level=None, fill_value=None)
Get Multiplication of dataframe and other, element-wise (binary operator mul).

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

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.
446-2、参数

446-2-1、other(必须)用于与调用DataFrame进行逐元素乘法运算的对象,可以是另一个DataFrame、Series、标量或常数,如果是DataFrame或Series,则其形状必须与调用的DataFrame兼容(即可以对齐)。

446-2-2、axis(可选,默认值为'columns') {0 or ‘index’, 1 or ‘columns’},确定对齐的轴,如果是0或'index',则沿着行对齐;如果是1或'columns',则沿着列对齐。

446-2-3、level(可选,默认值为None)如果指定了多级索引(MultiIndex),则在指定的级别上进行对齐,默认是None,即不使用多级索引。

446-2-4、fill_value(可选,默认值为None)用于替换DataFrame中缺失的数据(即NaN)的值,如果在运算中发现NaN值,它们将被fill_value替换,然后再进行运算,默认是None,即不替换NaN值。

446-3、功能

        用于将一个DataFrame与另一个DataFrame、Series或标量进行逐元素的乘法运算,其结果是一个新的DataFrame,其中每个元素是对应位置上两个操作数的乘积。

446-4、返回值

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

446-5、说明

        无

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

# 446-2、与另一个DataFrame的乘法运算
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
other = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df.mul(other)
print(result, end='\n\n')

# 446-3、使用fill_value参数
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, None],
    'B': [4, None, 6]
})
df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df1.mul(df2, fill_value=1)
print(result)
446-6-3、结果输出
# 446、pandas.DataFrame.mul方法
# 446-1、与标量的乘法运算
#    A   B
# 0  2   8
# 1  4  10
# 2  6  12

# 446-2、与另一个DataFrame的乘法运算
#     A    B
# 0  10  160
# 1  40  250
# 2  90  360

# 446-3、使用fill_value参数
#       A      B
# 0  10.0  160.0
# 1  40.0   50.0
# 2  30.0  360.0
447、pandas.DataFrame.div方法
447-1、语法
# 447、pandas.DataFrame.div方法
pandas.DataFrame.div(other, axis='columns', level=None, fill_value=None)
Get Floating division of dataframe and other, element-wise (binary operator truediv).

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

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.
447-2、参数

447-2-1、other(必须)用于与调用DataFrame进行逐元素除法运算的对象,可以是另一个DataFrame、Series、标量或常数,如果是DataFrame或Series,则其形状必须与调用的DataFrame兼容(即可以对齐)。

447-2-2、axis(可选,默认值为'columns') {0 or ‘index’, 1 or ‘columns’},确定对齐的轴,如果是0或'index',则沿着行对齐;如果是1或'columns',则沿着列对齐。

447-2-3、level(可选,默认值为None)如果指定了多级索引(MultiIndex),则在指定的级别上进行对齐,默认是None,即不使用多级索引。

447-2-4、fill_value(可选,默认值为None)用于替换DataFrame中缺失的数据(即NaN)的值,如果在运算中发现NaN值,它们将被fill_value替换,然后再进行运算,默认是None,即不替换NaN值。

447-3、功能

        用于将一个DataFrame与另一个DataFrame、Series或标量进行逐元素的除法运算,其结果是一个新的DataFrame,其中每个元素是对应位置上两个操作数的商。

447-4、返回值

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

447-5、说明

        无

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

# 447-2、与另一个DataFrame的除法运算
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
other = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df.div(other)
print(result, end='\n\n')

# 447-3、使用fill_value参数
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, None],
    'B': [4, None, 6]
})
df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df1.div(df2, fill_value=1)
print(result)
447-6-3、结果输出
# 447、pandas.DataFrame.div方法
# 447-1、与标量的除法运算
#      A    B
# 0  0.5  2.0
# 1  1.0  2.5
# 2  1.5  3.0

# 447-2、与另一个DataFrame的除法运算
#      A    B
# 0  0.1  0.1
# 1  0.1  0.1
# 2  0.1  0.1

# 447-3、使用fill_value参数
#           A     B
# 0  0.100000  0.10
# 1  0.100000  0.02
# 2  0.033333  0.10
448、pandas.DataFrame.truediv方法
448-1、语法
# 448、pandas.DataFrame.truediv方法
pandas.DataFrame.truediv(other, axis='columns', level=None, fill_value=None)
Get Floating division of dataframe and other, element-wise (binary operator truediv).

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

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.
448-2、参数

448-2-1、other(必须)用于与调用DataFrame进行逐元素除法运算的对象,可以是另一个DataFrame、Series、标量或常数,如果是DataFrame或Series,则其形状必须与调用的DataFrame兼容(即可以对齐)。

448-2-2、axis(可选,默认值为'columns') {0 or ‘index’, 1 or ‘columns’},确定对齐的轴,如果是0或'index',则沿着行对齐;如果是1或'columns',则沿着列对齐。

448-2-3、level(可选,默认值为None)如果指定了多级索引(MultiIndex),则在指定的级别上进行对齐,默认是None,即不使用多级索引。

448-2-4、fill_value(可选,默认值为None)用于替换DataFrame中缺失的数据(即NaN)的值,如果在运算中发现NaN值,它们将被fill_value替换,然后再进行运算,默认是None,即不替换NaN值。

448-3、功能

        用于将一个DataFrame与另一个DataFrame、Series或标量执行逐元素除法操作,该方法返回的结果是一个新的DataFrame,其中每个元素是两个操作数对应位置的商。

448-4、返回值

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

448-5、说明

        无

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

# 448-2、与另一个DataFrame的真除法运算
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
other = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df.truediv(other)
print(result, end='\n\n')

# 448-3、使用fill_value参数处理缺失值
import pandas as pd
df1 = pd.DataFrame({
    'A': [1, 2, None],
    'B': [4, None, 6]
})
df2 = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [40, 50, 60]
})
result = df1.truediv(df2, fill_value=1)
print(result)
448-6-3、结果输出
# 448、pandas.DataFrame.truediv方法
# 448-1、与标量的真除法运算
#      A    B
# 0  0.5  2.0
# 1  1.0  2.5
# 2  1.5  3.0

# 448-2、与另一个DataFrame的真除法运算
#      A    B
# 0  0.1  0.1
# 1  0.1  0.1
# 2  0.1  0.1

# 448-3、使用fill_value参数处理缺失值
#           A     B
# 0  0.100000  0.10
# 1  0.100000  0.02
# 2  0.033333  0.10
449、pandas.DataFrame.floordiv方法
449-1、语法
# 449、pandas.DataFrame.floordiv方法
pandas.DataFrame.floordiv(other, axis='columns', level=None, fill_value=None)
Get Integer division of dataframe and other, element-wise (binary operator floordiv).

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

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.
449-2、参数

449-2-1、other(必须)用于进行地板除法操作的对象,可以是另一个DataFrame、Series、标量或常数,如果是DataFrame或Series,则它们的形状应与调用的DataFrame兼容,以便进行元素级别的对齐。

449-2-2、axis(可选,默认值为'columns') {0 or ‘index’, 1 or ‘columns’},确定对齐的轴,如果是0或'index',则沿着行对齐;如果是1或'columns',则沿着列对齐。

449-2-3、level(可选,默认值为None)如果指定了多级索引(MultiIndex),则在指定的级别上进行对齐,默认是None,即不使用多级索引。

449-2-4、fill_value(可选,默认值为None)用于替换DataFrame中缺失的数据(即NaN)的值,如果在运算中发现NaN值,它们将被fill_value替换,然后再进行运算,默认是None,即不替换NaN值。

449-3、功能

        用于将一个DataFrame与另一个DataFrame、Series或标量执行逐元素地板除法操作,该方法返回的结果是一个新的DataFrame,其中每个元素是两个操作数对应位置的整数商。

449-4、返回值

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

449-5、说明

        无

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

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

# 449-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.floordiv(df2, fill_value=1)
print(result)
449-6-3、结果输出
# 449、pandas.DataFrame.floordiv方法
# 449-1、与标量的地板除法运算
#    A  B
# 0  0  2
# 1  1  2
# 2  1  3

# 449-2、与另一个DataFrame的地板除法运算
#    A  B
# 0  1  2
# 1  1  2
# 2  1  3

# 449-3、使用fill_value参数处理缺失值
#      A    B
# 0  0.0  2.0
# 1  1.0  0.0
# 2  0.0  3.0
450、pandas.DataFrame.mod方法
450-1、语法
# 450、pandas.DataFrame.mod方法
pandas.DataFrame.mod(other, axis='columns', level=None, fill_value=None)
Get Modulo of dataframe and other, element-wise (binary operator mod).

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

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.
450-2、参数

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

450-2-2、axis(可选,默认值为'columns') {0 or ‘index’, 1 or ‘columns’},确定对齐的轴,如果是0或'index',则沿着行对齐;如果是1或'columns',则沿着列对齐。

450-2-3、level(可选,默认值为None)如果指定了多级索引(MultiIndex),则在指定的级别上进行对齐,默认是None,即不使用多级索引。

450-2-4、fill_value(可选,默认值为None)用于替换DataFrame中缺失的数据(即NaN)的值,如果在运算中发现NaN值,它们将被fill_value替换,然后再进行运算,默认是None,即不替换NaN值。

450-3、功能

        用于逐元素地对DataFrame进行取模运算(即求余数),该方法返回一个新的DataFrame,其中每个元素是调用DataFrame与other对应位置元素的余数。

450-4、返回值

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

450-5、说明

        无

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

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

# 450-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.mod(df2, fill_value=1)
print(result)
450-6-3、结果输出
# 450、pandas.DataFrame.mod方法
# 450-1、与标量的取模运算
#    A  B
# 0  1  0
# 1  0  1
# 2  1  0

# 450-2、与另一个DataFram的取模运算
#    A  B
# 0  0  0
# 1  0  1
# 2  0  0

# 450-3、使用fill_value参数处理缺失值
#      A    B
# 0  1.0  0.0
# 1  0.0  1.0
# 2  1.0  0.0

二、推荐阅读

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

http://www.kler.cn/a/527292.html

相关文章:

  • Redis 基础命令
  • SCRM开发为企业提供全面客户管理解决方案与创新实践分享
  • 二级C语言:二维数组每行最大值与首元素交换、删除结构体的重复项、取出单词首字母
  • 【C语言】内存管理
  • 洛谷P2651 添加括号III
  • 我的创作纪念日——成为创作者的 第365天(1年)
  • Spring RESTful API 设计与实现
  • 使用openAI与Deepseek的感受
  • 安心即美的生活方式
  • 2025-1-26-sklearn学习(46) 无监督学习: 寻求数据表示 空伫立,尽日阑干倚遍,昼长人静。
  • Native Memory Tracking 与 RSS的差异问题
  • 验证二叉搜索数(98)
  • 【算法】动态规划专题① ——线性DP python
  • 理解动手学深度学习的自编包d2l
  • 青少年编程与数学 02-008 Pyhon语言编程基础 05课题、数据类型
  • 【Elasticsearch】match_bool_prefix 查询 vs match_phrase_prefix 查询
  • DeepSeek的使用技巧介绍
  • SARIMA介绍
  • 【TCP协议】流量控制 滑动窗口
  • 高速PCB设计指南5——电磁干扰和电磁兼容