HomeCourseModule 09 › Applying functions — apply, map, vectorisation

Applying functions — apply, map, vectorisation

Module 09 · Pandas Crash Course7 min readIntermediate

What you'll learn

  • Use .apply() on a Series and a DataFrame
  • Use .map() for simple value-to-value substitutions
  • Prefer vectorised operations when possible

The hierarchy: vectorised > apply > loop

The fastest pandas code uses built-in vectorised operations. Reach for apply when there's no built-in, and only loop manually as a last resort.

Vectorised — the goal

df["total"] = df["price"] * df["quantity"] * (1 + df["tax_rate"])

.map() — value-to-value lookup on a Series

tier_label = {"A": "Gold", "B": "Silver", "C": "Bronze"}
df["tier_name"] = df["tier"].map(tier_label)

.apply() on a Series

def classify(amount):
    if amount > 1000: return "Big"
    if amount > 100:  return "Medium"
    return "Small"

df["bucket"] = df["amount"].apply(classify)

.apply() on a DataFrame (row-wise)

def full_label(row):
    return f"{row['region']}/{row['product']}"

df["label"] = df.apply(full_label, axis=1)

The vectorised replacement

Almost anything you'd write with apply has a faster vectorised form:

# Slower
df["bucket"] = df["amount"].apply(classify)

# Faster
import numpy as np
df["bucket"] = np.select(
    [df["amount"] > 1000, df["amount"] > 100],
    ["Big", "Medium"],
    default="Small")
💡 The lazy rule
Use apply when you're prototyping. Switch to vectorised when the data is large and performance matters.

Key takeaways

  • Vectorised pandas is fast; apply is slow; manual loops are slowest.
  • .map(dict) is the cleanest way to translate values.
  • .apply(func, axis=1) runs func(row) on each row.

Tier classification

Given a column of customer LTV values, add a tier column: Gold > 5000, Silver > 1000, Bronze otherwise. Solve with apply; then rewrite with np.select.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.