Arithmetic Operations

Back

Loading concept...

🧮 Pandas Arithmetic Operations: Your Data’s Calculator

Imagine you have a magical calculator that can do math on thousands of numbers at once. That’s what Pandas arithmetic is!


🎯 What’s This All About?

Think of your data like a classroom full of students. Each student has a test score. What if you wanted to:

  • Give everyone 5 bonus points?
  • Double everyone’s score?
  • Compare two classrooms?

With Pandas, you can do ALL of this with just ONE line of code! No loops, no fuss.


📊 Series Arithmetic: Math on a Single Column

A Series is like a single column of numbers — imagine a list of prices at a candy store.

The Magic: Operations Apply to EVERY Item

import pandas as pd

# Candy prices in dollars
prices = pd.Series([1.50, 2.00, 0.75, 3.25])

# Add 0.50 to EVERY price (tax!)
prices + 0.50
# Result: [2.00, 2.50, 1.25, 3.75]

# Double all prices
prices * 2
# Result: [3.00, 4.00, 1.50, 6.50]

Series + Series = Element-by-Element Math

costs = pd.Series([1.00, 1.50, 0.50, 2.00])
markup = pd.Series([0.50, 0.50, 0.25, 1.25])

# Final price = cost + markup
final_price = costs + markup
# Result: [1.50, 2.00, 0.75, 3.25]

🎪 Analogy: It’s like two lines of kids pairing up. Kid 1 from line A pairs with Kid 1 from line B. They add their candies together!


📋 DataFrame Arithmetic: Math on Tables

A DataFrame is a whole table — like a spreadsheet with rows AND columns.

Scalar Operations (One Number to All)

# Student grades
grades = pd.DataFrame({
    'Math': [85, 90, 78],
    'Science': [88, 92, 85]
})

# Curve: Add 5 points to EVERYTHING
curved = grades + 5
# Math becomes [90, 95, 83]
# Science becomes [93, 97, 90]

DataFrame + DataFrame

semester1 = pd.DataFrame({
    'Math': [80, 85],
    'Science': [75, 90]
})

semester2 = pd.DataFrame({
    'Math': [85, 90],
    'Science': [80, 88]
})

# Total points = semester1 + semester2
total = semester1 + semester2
# Math: [165, 175]
# Science: [155, 178]

🏠 Analogy: Imagine two identical houses. You’re counting windows in each room. DataFrame arithmetic lets you add up windows room-by-room, floor-by-floor, all at once!


🔧 Arithmetic Methods: The Named Tools

Sometimes using +, -, *, / isn’t enough. Pandas gives you methods with superpowers!

Basic Methods

Operator Method What It Does
+ .add() Addition
- .sub() Subtraction
* .mul() Multiplication
/ .div() Division
** .pow() Power/Exponent
// .floordiv() Floor Division
% .mod() Remainder

The fill_value Superpower

What happens when data is missing? Methods let you handle it!

a = pd.Series([1, 2, None, 4])
b = pd.Series([10, 20, 30, 40])

# Regular addition: None + 30 = NaN
a + b  # [11, 22, NaN, 44]

# Using fill_value: treat None as 0
a.add(b, fill_value=0)
# [11, 22, 30, 44] ✨

🧩 Analogy: fill_value is like having a substitute player. When someone is absent, the substitute jumps in!


🚀 Advanced Arithmetic Operations

Reverse Operations

Sometimes you want number - Series instead of Series - number:

prices = pd.Series([10, 20, 30])

# Regular: prices - 5 = [5, 15, 25]
prices - 5

# Reverse: 100 - prices = [90, 80, 70]
prices.rsub(100)  # "reverse subtract"

Combining Multiple Operations

# Calculate final price with tax and discount
base_price = pd.Series([100, 200, 150])
tax_rate = 0.08
discount = 10

final = base_price.mul(1 + tax_rate).sub(discount)
# Step 1: 100 * 1.08 = 108
# Step 2: 108 - 10 = 98
# Result: [98, 206, 152]

Operations with Different Indexes

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

result = s1 + s2
# a: NaN (only in s1)
# b: 2 + 10 = 12
# c: 3 + 20 = 23
# d: NaN (only in s2)

🎭 Analogy: It’s like matching dance partners by name tags. Only people with matching tags can dance together. Everyone else sits out (NaN)!


📡 Broadcasting in Pandas

Broadcasting is Pandas’ way of being smart about mismatched shapes.

Series + Scalar (Simplest Broadcasting)

scores = pd.Series([70, 80, 90])
bonus = 5

# The single number "broadcasts" to all
scores + bonus  # [75, 85, 95]

DataFrame + Series (Column-wise)

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

row_adjustment = pd.Series([10, 100], index=['A', 'B'])

# Series broadcasts across ROWS
df + row_adjustment
# A: [11, 12, 13]
# B: [104, 105, 106]

Row-wise Broadcasting with axis=0

df = pd.DataFrame({
    'Math': [80, 90],
    'Science': [85, 88]
})

student_bonus = pd.Series([5, 10])  # Per student

# Add different bonus to each ROW
df.add(student_bonus, axis=0)
# Row 0: Math=85, Science=90
# Row 1: Math=100, Science=98
graph TD A["Small Shape"] --> B["Broadcasting Engine"] C["Large Shape"] --> B B --> D["Matched Shapes"] D --> E["Element-wise Operation"] E --> F["Result!"]

🎈 Analogy: Broadcasting is like a balloon that expands to fill any space. A single number “inflates” to match the bigger shape!


🎯 Quick Summary

Concept What It Does Example
Series Arithmetic Math on single columns prices * 1.1
DataFrame Arithmetic Math on tables grades + 5
Arithmetic Methods Named operations with options .add(fill_value=0)
Advanced Operations Reverse ops, chaining .rsub(100)
Broadcasting Smart shape matching df + series

💡 Remember This!

  1. Operations apply to ALL elements — no loops needed!
  2. Matching happens by index — mismatched indexes give NaN
  3. Methods give you fill_value — handle missing data gracefully
  4. Broadcasting is automatic — Pandas figures out the shape

You’ve just learned how to make thousands of calculations with single lines of code. That’s the power of Pandas arithmetic! 🎉

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.