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(无穷大)。这些基本数值运算对于数据分析和处理至关重要。

相关推荐
liuweidong08022 小时前
【Pandas】pandas DataFrame ffill
pandas
vvilkim3 小时前
Pandas 可视化集成:数据科学家的高效绘图指南
信息可视化·pandas
Gyoku Mint3 天前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
坚持就完事了3 天前
大二下期末
python·numpy·pandas
仟濹4 天前
「数据分析 - Pandas 函数」【数据分析全栈攻略:爬虫+处理+可视化+报告】
爬虫·数据分析·pandas
KENYCHEN奉孝4 天前
Pandas和Django的示例Demo
python·django·pandas
liuweidong08025 天前
【Pandas】pandas DataFrame sample
python·数据挖掘·pandas
java1234_小锋5 天前
一周学会Pandas2之Python数据处理与分析-Pandas2数据绘图与可视化
开发语言·python·信息可视化·pandas
先做个垃圾出来………6 天前
Python中使用pandas
开发语言·python·pandas