Data Aggregation

Python · GroupBy & SQL-Style Analytics

Theory

Aggregation collapses many values into one summary number. On a column (a Series) you call methods such as sum(), mean(), median(), min(), max(), std(), count() (non-missing values) and nunique() (distinct values). df['salary'].mean() returns the average salary; df['salary'].sum() the total. On a whole DataFrame, df.sum(numeric_only=True) gives one result per numeric column.

To compute several at once use agg(): df['salary'].agg(['sum', 'mean', 'min', 'max', 'count']) returns a small Series with one entry per function, and df.agg({'salary': ['sum', 'mean'], 'name': 'count'}) applies different functions per column. df.describe() gives count, mean, std, min, quartiles and max for every numeric column in one call. pandas skips NaN values by default, so mean() averages only the known values. When you need these summaries per category rather than for the whole column, use groupby().

Why we use it

Raw rows rarely answer business questions; summaries do - total revenue, average order value, number of customers. Aggregation methods compute them reliably in one line.

When to use it

Use them for whole-column summaries and quick sanity checks after loading data. For per-group summaries (per department, per month) combine them with groupby(); for a full statistical profile use describe().

How it works

  1. df['salary']
  2. Choose the summary: sum, mean, count ...
  3. Skip NaN values (default skipna=True)
  4. Several summaries needed? Yes: Use .agg(['sum', 'mean', ...]). No: Call one method like .mean().
  5. Summary value(s)

Syntax

df['col'].sum() / .mean() / .count() / .nunique()
df['col'].agg(['sum', 'mean', 'max'])
df.agg({'col1': 'sum', 'col2': ['min', 'max']})

Example

print(df['salary'].mean())
print(df['salary'].agg(['min', 'max', 'mean']))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic