2️⃣ 🧱 Pandas Core Data Structures
Estimated reading: 3 minutes 28 views

➕ Pandas Arithmetic Operations on DataFrames – Perform Fast Table-Wide Math


🧲 Introduction – Why Perform Arithmetic on DataFrames?

Arithmetic operations on Pandas DataFrames allow you to manipulate entire columns and rows efficiently—ideal for data transformation, feature engineering, and statistical analysis. You can perform element-wise operations, broadcast values, and combine multiple DataFrames with ease.

🎯 In this guide, you’ll learn:

  • How to add, subtract, multiply, and divide columns or rows
  • Perform arithmetic between DataFrames
  • Handle missing values and index alignment
  • Apply custom and NumPy-based arithmetic functions

➗ 1. Element-wise Arithmetic Between Columns

import pandas as pd

df = pd.DataFrame({
    'Price': [100, 200, 300],
    'Tax': [10, 20, 30]
})

df['Total'] = df['Price'] + df['Tax']
print(df)

👉 Output:

   Price  Tax  Total
0    100   10    110
1    200   20    220
2    300   30    330

✅ Operations are element-wise and apply to entire columns.


🧮 2. Arithmetic with Scalar Values

df['Price_After_Discount'] = df['Price'] * 0.9

✅ Scalars are broadcasted across the entire column.


🔁 3. Arithmetic Between Two DataFrames

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})

result = df1 + df2
print(result)

👉 Output:

    A   B
0  11  33
1  22  44

✅ DataFrames align by both row index and column labels.


❓ 4. Mismatched Index or Columns

df3 = pd.DataFrame({'A': [1, 2]}, index=[0, 1])
df4 = pd.DataFrame({'B': [10, 20]}, index=[0, 2])

print(df3 + df4)

👉 Output:

     A   B
0  NaN  NaN
1  NaN  NaN
2  NaN  NaN

✅ Result contains NaN where alignment fails.


🛠️ 5. Fill Missing Values with fill_value

result = df3.add(df4, fill_value=0)
print(result)

fill_value=0 replaces missing values before operation.


📊 6. Row-wise and Column-wise Operations

➕ Row-wise (axis=1):

df['Sum'] = df.sum(axis=1)

➕ Column-wise (axis=0):

column_total = df.sum(axis=0)

✅ Specify axis to control direction of aggregation or arithmetic.


🔢 7. Arithmetic with NumPy Functions

import numpy as np

df['Sqrt_Price'] = np.sqrt(df['Price'])
df['Log_Tax'] = np.log(df['Tax'])

✅ Pandas works seamlessly with NumPy’s vectorized math functions.


📐 8. Chained Arithmetic Expressions

df['Final'] = ((df['Price'] - df['Tax']) * 1.05).round(2)

✅ Combine multiple operations like in algebraic formulas.


📌 Summary – Recap & Next Steps

Pandas makes arithmetic on tabular data intuitive, efficient, and readable. Whether you’re combining columns, adjusting values, or merging DataFrames—arithmetic operations are key to data transformation and analysis.

🔍 Key Takeaways:

  • Use +, -, *, / operators directly on columns or DataFrames
  • Pandas auto-aligns by index and column labels
  • Use fill_value to handle NaNs in misaligned DataFrames
  • Leverage NumPy functions for advanced math

⚙️ Real-world relevance: Used in budget modeling, sales forecasts, risk analysis, and machine learning preprocessing.


❓ FAQs – DataFrame Arithmetic in Pandas

❓ Can I apply arithmetic operations to specific rows or columns?
✅ Yes. Use slicing or column selection before applying arithmetic.

❓ How do I avoid NaN when combining DataFrames?
Use:

df1.add(df2, fill_value=0)

❓ Can I use math functions like log, sqrt, or exp?
✅ Yes. Use NumPy:

np.log(df['Column'])

❓ Does the original DataFrame change after arithmetic?
❌ No. Unless reassigned or inplace=True, original data remains unchanged.

❓ Can I subtract a row or column from the entire DataFrame?
✅ Yes. Pandas supports broadcasting:

df - df.iloc[0]   # Subtract first row

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Pandas Arithmetic Operations on DataFrames

Or Copy Link

CONTENTS
Scroll to Top