首页 > Python资料 博客日记
Python酷库之旅-第三方库Pandas(103)
2024-09-08 20:00:11Python资料围观78次
目录
448、pandas.DataFrame.truediv方法
449、pandas.DataFrame.floordiv方法
一、用法精讲
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、博客个人主页
标签:
相关文章
最新发布
- 光流法结合深度学习神经网络的原理及应用(完整代码都有Python opencv)
- Python 图像处理进阶:特征提取与图像分类
- 大数据可视化分析-基于python的电影数据分析及可视化系统_9532dr50
- 【Python】入门(运算、输出、数据类型)
- 【Python】第一弹---解锁编程新世界:深入理解计算机基础与Python入门指南
- 华为OD机试E卷 --第k个排列 --24年OD统一考试(Java & JS & Python & C & C++)
- Python已安装包在import时报错未找到的解决方法
- 【Python】自动化神器PyAutoGUI —告别手动操作,一键模拟鼠标键盘,玩转微信及各种软件自动化
- Pycharm连接SQL Sever(详细教程)
- Python编程练习题及解析(49题)
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应
- Windows上安装 Python 环境并配置环境变量 (超详细教程)
- Python pyinstaller打包exe最完整教程