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、语法
python 复制代码
# 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、数据准备
python 复制代码
446-6-2、代码示例
python 复制代码
# 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、结果输出
python 复制代码
# 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、语法
python 复制代码
# 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、数据准备
python 复制代码
447-6-2、代码示例
python 复制代码
# 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、结果输出
python 复制代码
# 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、语法
python 复制代码
# 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、数据准备
python 复制代码
448-6-2、代码示例
python 复制代码
# 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、结果输出
python 复制代码
# 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、语法
python 复制代码
# 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、数据准备
python 复制代码
449-6-2、代码示例
python 复制代码
# 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、结果输出
python 复制代码
# 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、语法
python 复制代码
# 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、数据准备
python 复制代码
450-6-2、代码示例
python 复制代码
# 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、结果输出
python 复制代码
# 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、博客个人主页
相关推荐
zm-v-159304339861 小时前
Python 数据挖掘从入门到精通:回归 / 分类 / 聚类 / 关联分析完整教程
python·数据挖掘·回归
新缸中之脑4 小时前
Paperless-NGX实战文档管理
人工智能
似水明俊德6 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
qq_417695056 小时前
机器学习与人工智能
jvm·数据库·python
无极低码6 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
漫随流水6 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
grant-ADAS6 小时前
记录paddlepaddleOCR从环境到使用默认模型,再训练自己的数据微调模型再推理
人工智能·深度学习
炎爆的土豆翔6 小时前
OpenCV 阈值二值化优化实战:LUT 并行、手写 AVX2 与 cv::threshold 性能对比
人工智能·opencv·计算机视觉
智能相对论7 小时前
从AWE看到海尔智慧家庭步步引领
人工智能
Thera7777 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++