Pandas实战100例 | 案例 49: 数值运算

案例 49: 数值运算

知识点讲解

Pandas 提供了进行基本数学运算的简便方法,允许你在 DataFrame 的列之间执行加法、减法、乘法和除法等操作。

  • 数值运算 : 直接对 DataFrame 的列应用算术运算符(+, -, *, /)可以执行相应的数值运算。
示例代码
python 复制代码
# 准备数据和示例代码的运行结果,用于案例 49

# 示例数据
data_numeric_operations = {
    'A': [10, 20, 30],
    'B': [5, 0, 15]
}
df_numeric_operations = pd.DataFrame(data_numeric_operations)

# 数值运算
df_numeric_operations['Sum'] = df_numeric_operations['A'] + df_numeric_operations['B']
df_numeric_operations['Difference'] = df_numeric_operations['A'] - df_numeric_operations['B']
df_numeric_operations['Product'] = df_numeric_operations['A'] * df_numeric_operations['B']
df_numeric_operations['Quotient'] = df_numeric_operations['A'] / df_numeric_operations['B']

df_numeric_operations

在这个示例中,我们对两个数值列 AB 进行了基本的数学运算,包括求和、求差、求积和求商。

示例代码运行结果
复制代码
    A   B  Sum  Difference  Product  Quotient
0  10   5   15           5       50       2.0
1  20   0   20          20        0       inf
2  30  15   45          15      450       2.0

这个结果显示了每种运算的结果。请注意,当除数为 0 时,结果为 inf(无穷大)。这些基本数值运算对于数据分析和处理至关重要。

相关推荐
悟乙己28 分钟前
PySpark 与 Pandas 的较量:Databricks 中 SQL Server 到 Snowflake 的数据迁移之旅
数据库·pandas·pyspark
九章云极AladdinEdu2 小时前
临床数据挖掘与分析:利用GPU加速Pandas和Scikit-learn处理大规模数据集
人工智能·pytorch·数据挖掘·pandas·scikit-learn·paddlepaddle·gpu算力
万粉变现经纪人17 小时前
如何解决pip安装报错ModuleNotFoundError: No module named ‘python-dateutil’问题
开发语言·ide·python·pycharm·pandas·pip·httpx
dlraba80217 小时前
机器学习实战(二):Pandas 特征工程与模型协同进阶
人工智能·机器学习·pandas
Source.Liu19 小时前
【Python自动化】 21.3 Pandas Series 核心数据结构完全指南
python·自动化·pandas
亦良Cool19 小时前
001-Pandas的数据结构
数据结构·pandas
CodeCraft Studio3 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
njxiejing3 天前
Pandas数据结构(DataFrame,字典赋值)
数据结构·人工智能·pandas
Calihen的学习日志4 天前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
Source.Liu5 天前
【Python自动化】 21.2 Pandas 读取 Excel 时的 dtype 参数完全指南
python·自动化·pandas