Grouping and Aggregating

Split data into groups, apply a function to each, and combine the results.

Syntaxdf.groupby('col')['value'].mean()

groupby follows a split-apply-combine pattern: split rows into groups by a column, compute a summary for each group, then combine into a new table.

Common aggregations

After grouping, call .mean(), .sum(), .count(), or use .agg() for several at once.

Example

Example · python
import pandas as pd

df = pd.DataFrame({
    'city': ['Paris', 'Rome', 'Rome', 'Paris'],
    'sales': [100, 200, 150, 50],
})

print(df.groupby('city')['sales'].sum())
# city
# Paris    150
# Rome     350

print(df.groupby('city')['sales'].agg(['sum', 'mean', 'count']))

When to use it

  • A business analyst groups a sales DataFrame by region and product category to compute total revenue per combination for a quarterly report.
  • An ML engineer groups training data by class label to count samples per class and detect severe class imbalance before oversampling.
  • A data scientist uses groupby followed by transform to fill each group's missing values with that group's mean, preserving per-category context.

More examples

Group and aggregate a column

Groups rows by region and computes both total and average sales per group, the most common groupby pattern.

Example · python
import pandas as pd

df = pd.DataFrame({
    'region': ['East', 'West', 'East', 'West', 'East'],
    'sales':  [200, 340, 150, 410, 275]
})
print(df.groupby('region')['sales'].sum())
print(df.groupby('region')['sales'].mean())

Multiple aggregations with agg

Applies three aggregation functions at once with agg, returning sum, mean, and count of orders per category in one call.

Example · python
import pandas as pd

df = pd.read_csv('orders.csv')
summary = df.groupby('category')['amount'].agg(['sum', 'mean', 'count'])
print(summary)

Class balance check for ML

Uses groupby with size() to count samples per class label and then converts counts to percentages, revealing class imbalance before model training.

Example · python
import pandas as pd

df = pd.read_csv('churn.csv')
class_counts = df.groupby('label').size()
print(class_counts)
print((class_counts / len(df) * 100).round(1))

Discussion

  • Be the first to comment on this lesson.