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

➕ Pandas Arithmetic Operations on Series – Perform Element-wise Math with Ease


🧲 Introduction – Why Perform Arithmetic on Series?

Arithmetic operations on Pandas Series allow you to perform vectorized, element-wise computations across numerical data with ease. Whether you’re scaling data, combining Series, or applying formulas—Pandas makes it fast, readable, and efficient.

🎯 In this guide, you’ll learn:

  • How to perform element-wise arithmetic (add, subtract, multiply, divide)
  • Alignment behavior when combining Series
  • Using NumPy and math functions with Series
  • Handling mismatched indexes and missing values

➗ 1. Basic Arithmetic Operations

import pandas as pd

s1 = pd.Series([10, 20, 30])
s2 = pd.Series([1, 2, 3])

print(s1 + s2)    # Addition
print(s1 - s2)    # Subtraction
print(s1 * s2)    # Multiplication
print(s1 / s2)    # Division

👉 Output:

0    11
1    22
2    33
dtype: int64

0     9
1    18
2    27
dtype: int64

0     10
1     40
2     90
dtype: int64

0    10.0
1    10.0
2    10.0
dtype: float64

✅ Operations are element-wise and broadcast automatically.


🧾 2. Operations with Scalars

s = pd.Series([100, 200, 300])

print(s + 10)     # Add 10 to each element
print(s * 2)      # Multiply each element by 2

👉 Output:

0    110
1    210
2    310
dtype: int64

0    200
1    400
2    600
dtype: int64

✅ No need for loops. Scalars are automatically applied to all elements.


🧮 3. Series with Custom Index (Auto Alignment)

s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s2 = pd.Series([1, 2, 3], index=['b', 'c', 'd'])

print(s1 + s2)

👉 Output:

a     NaN
b    21.0
c    22.0
d     NaN
dtype: float64

✅ Pandas aligns on index labels, not positions
✅ Missing matches result in NaN by default


🧰 4. Fill Missing Values in Arithmetic

Use fill_value in add(), sub(), mul(), etc.

print(s1.add(s2, fill_value=0))

👉 Output:

a    10.0
b    21.0
c    22.0
d     3.0
dtype: float64

✅ Replaces NaN with 0 before applying the operation.


🧠 5. Mathematical & NumPy Functions on Series

import numpy as np

s = pd.Series([1, 4, 9, 16])

print(np.sqrt(s))         # Square root
print(np.log(s))          # Natural log
print(np.exp(s))          # Exponentiation

✅ Pandas Series work seamlessly with NumPy’s vectorized functions


📋 6. Comparison and Logical Operations

s = pd.Series([10, 15, 20])

print(s > 12)     # Greater than
print(s == 15)    # Equal to

👉 Output:

0    False
1     True
2     True
dtype: bool

0    False
1     True
2    False
dtype: bool

✅ Returns Boolean Series—useful for filtering


🔄 7. Chained Arithmetic Operations

s = pd.Series([100, 200, 300])

result = (s - 50) * 2 / 5
print(result)

👉 Output:

0    20.0
1    60.0
2    100.0
dtype: float64

✅ Arithmetic operations can be chained like algebraic expressions


📌 Summary – Recap & Next Steps

Arithmetic operations on Pandas Series allow fast, readable, and vectorized mathematical transformations. Whether working with raw numbers, comparing values, or applying formulas—Series behaves just like NumPy arrays with additional label-awareness.

🔍 Key Takeaways:

  • Use +, -, *, / directly on Series for element-wise math
  • Series auto-aligns on indexes—watch for NaNs
  • Use fill_value to handle misaligned indexes
  • NumPy math functions integrate natively with Series

⚙️ Real-world relevance: Perfect for column-wise transformations in datasets, feature scaling in ML, financial calculations, and more.


❓ FAQs – Pandas Arithmetic on Series

❓ What happens if indexes don’t match?
✅ Pandas aligns by label. If labels don’t match, result is NaN.

❓ How do I avoid NaN in mismatched Series?
Use add(), sub(), etc. with fill_value:

s1.add(s2, fill_value=0)

❓ Can I use NumPy functions directly on Series?
✅ Yes! Series supports NumPy vectorized functions like np.sqrt(), np.log(), etc.

❓ Do arithmetic operations affect the original Series?
❌ No. They return new Series. Originals are unchanged unless reassigned.

❓ Can I perform arithmetic with Series and DataFrames?
✅ Yes, but they must align in index and/or columns depending on context.


Share Now :

Leave a Reply

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

Share

Pandas Arithmetic Operations on Series

Or Copy Link

CONTENTS
Scroll to Top