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

相关推荐
超龄超能程序猿4 天前
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
人工智能·python·机器学习·numpy·pandas·scipy
李昊哲小课5 天前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas
袁袁袁袁满5 天前
利用Pandas进行条件替换与向前填充
pandas·条件替换·向前填充
Deng9452013146 天前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术
liuweidong08026 天前
【Pandas】pandas DataFrame from_records
pandas
大数据魔法师7 天前
基于Pandas和FineBI的昆明职位数据分析与可视化实现(五) - 基于随机森林算法预测职位分类
算法·pandas
Chasing__Dreams8 天前
python--杂识--18.1--pandas数据插入sqlite并进行查询
python·sqlite·pandas
超龄超能程序猿9 天前
(3)机器学习小白入门 YOLOv: 解锁图片分类新技能
python·numpy·pandas·scipy
好开心啊没烦恼10 天前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
大虫小呓11 天前
Python 处理 Excel 数据 pandas 和 openpyxl 哪家强?
python·pandas