1
Step 4 Themes per Year
Tirsvad edited this page 2026-11-12 01:50:57 +01:00

English · Dansk · Bahasa Malaysia

Step 4: Themes per year with .agg()

Question: How many different themes did LEGO sell each year?

Step 4: Themes per year with .agg()

What the code does

themes_by_year runs sets.groupby("year").agg({"theme_id": pd.Series.nunique}) and renames the column to nr_themes. There were 2 themes in 1949 and 78 in 2019; themes_by_year_chart plots the full calendar years only.

What you gained

  • .agg() takes a dictionary: the key is the column, the value is the operation.
  • .rename(columns=...) gives a column a better name.
  • The number of themes grew until the mid-1990s, stalled, and grew again in the early 2010s.

Previous: Step-3-Sets-per-Year · Next: Step-5-Two-Axes