Python, from Zero, for AILesson 77 of 89

Module 8 · Numbers in bulk: NumPy, pandas and the vector under every model

groupby, merge and pivot: getting from rows to the table a question needs

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 8 of 10 in this chapter

This chapter’s goalLoad a messy CSV into pandas, clean its missing values and types, group and join it into the table a question needs, convert it to a NumPy array of the right shape and dtype, compute cosine similarity between one vector and a hundred thousand in a single vectorised expression, plot the result to a file, and explain from memory layout why the array version runs a hundred times faster than the loop it replaced.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:43Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Split, apply, combine

Every “per something” question is a groupby: revenue per month, average score per model, count of messages per language.

Python
df.groupby("model")["cost_usd"].sum()

Read it as three steps. Split the rows into groups by model. Apply sum to the cost_usd column of each group. Combine the results into a Series indexed by model. Several statistics at once:

Python
df.groupby("model").agg(
    calls=("cost_usd", "size"),
    total=("cost_usd", "sum"),
    mean_tokens=("output_tokens", "mean"),
    p95_latency=("latency_s", lambda s: s.quantile(0.95)),
)

Named aggregation (new_column=(source_column, function)) gives readable output columns. "size" counts rows including missing; "count" counts non-missing. A lambda works for anything without a built-in name, at the cost of speed.

Group by several keys and the result has a multi-level index:

Python
by = df.groupby(["month", "model"])["cost_usd"].sum()
by.reset_index()          # back to ordinary columns
by.unstack("model")       # months as rows, models as columns

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘groupby, merge and pivot: getting from rows to the table a question needs’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/groupby-merge-and-reshape (Accessed: 21 November 2026).