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

相关推荐
Hello.Reader9 小时前
Pandas API on Spark 快速入门像写 Pandas 一样使用 Spark
大数据·spark·pandas
白日与明月3 天前
Pandas 读取文本数据 (Text I/O) 速查表
爬虫·python·pandas
一晌小贪欢4 天前
Python办公自动化指南:Pandas与Openpyxl的全面比较与选择
开发语言·python·pandas·python基础·python入门·python小白
无心水4 天前
Python时间处理通关指南:datetime/arrow/pandas实战
开发语言·人工智能·python·pandas·datetime·arrow·金融科技
RE-19015 天前
Polars:告别 Pandas 性能瓶颈,用 Rust 驱动的 DataFrame 库处理亿级数据
开发语言·rust·pandas·polars·ai生成
B站_计算机毕业设计之家7 天前
计算机毕业设计:Python当当网图书数据全链路处理平台 Django框架 爬虫 Pandas 可视化 大数据 大模型 书籍(建议收藏)✅
爬虫·python·机器学习·django·flask·pandas·课程设计
万粉变现经纪人7 天前
如何解决 pip install shapely 报错 GEOS C 库未找到 问题
c语言·开发语言·python·pycharm·bug·pandas·pip
B站计算机毕业设计之家7 天前
计算机毕业设计源码:Python图书数据智能采集与可视化大屏 当当网 Django框架 爬虫 Pandas 可视化 大数据 大模型 书籍(建议收藏)✅
爬虫·python·机器学习·信息可视化·django·pandas·课程设计
小陈工8 天前
FastAPI性能优化实战:从每秒100请求到1000的踩坑记录
python·性能优化·django·flask·numpy·pandas·fastapi
deepxuan8 天前
Day1--python三大库-Pandas
人工智能·python·pandas