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

相关推荐
xiaotao1316 小时前
01-编程基础与数学基石:概率与统计
人工智能·python·numpy·pandas
人工干智能3 天前
科普:python的pandas包中的DataFrame就是二维表
开发语言·python·pandas
橙露3 天前
Polars 极速数据框架:比 Pandas 更快的大数据处理
pandas
Yu_Lijing4 天前
Python数据分析和数据处理库Pandas(Series篇)
人工智能·python·数据分析·pandas
AI效率工坊4 天前
【Python实战】10万行数据自动清洗:pandas+AI智能识别+异常检测完整方案
人工智能·python·pandas
人工干智能4 天前
科普:pandas 中的类 SQL语句:transaction.groupby(“card_id“)[‘purchase_day‘].diff()
数据库·sql·pandas
神秘剑客_CN4 天前
python安装requests及pandas
开发语言·python·pandas
zzwq.5 天前
Pandas读取数据:csv、excel、sql全攻略
python·pandas
zfan5205 天前
python对Excel数据处理(1)
python·excel·pandas
l1t6 天前
用wsl自带的python 3.10下载适用于3.12的pandas版本结合uv安装python 3.12模拟离线安装场景
python·pandas·uv