HomeCourseModule 09 › Aggregation — sum, mean, agg

Aggregation — sum, mean, agg

Module 09 · Pandas Crash Course8 min readBeginner

What you'll learn

  • Compute basic aggregates on a Series
  • Aggregate multiple columns at once
  • Use .agg() for multiple stats per column

Single-column aggregates

df["amount"].sum()
df["amount"].mean()
df["amount"].median()
df["amount"].std()
df["amount"].max()
df["amount"].min()
df["amount"].count()
df["amount"].quantile(0.95)

Whole-DataFrame aggregates

df.sum(numeric_only=True)
df.mean(numeric_only=True)
df.describe()

.agg() — many at once

df["amount"].agg(["sum", "mean", "min", "max"])

df.agg({
    "amount":   ["sum", "mean"],
    "quantity": ["sum", "max"],
})

Custom aggregations

def iqr(s):
    return s.quantile(0.75) - s.quantile(0.25)

df["amount"].agg(["mean", iqr])

Key takeaways

  • Series have direct methods: .sum(), .mean() etc.
  • .agg(["sum","mean"]) runs many at once.
  • .describe() is the quickest "show me everything" summary.

Summary box

For a numeric column of your choosing, print mean, median, min, max, and 95th percentile in a single .agg() call.

📹 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.