Skip to content
Start free

Read a filter and a group, line by line

Predict what each line of an AI's late-returns count prints, then say what it grouped and what it counted.

2 parts · 3 and 3 min

Free course. Your Maestro, an AI, .

On the last page, the support log printed 10 rows when only 4 tickets existed. Before looking at the new case, recall: what did one row of that support log stand for, and how did you tell? Answer in one sentence before you read on.

Checking the of a table tells you what one row represents. Your next step is reading the code an AI writes, line by line, so you can trace which rows survive a filter, how they get grouped, and what the final function counts.

A library coordinator asked an AI which branch had more late returns. The AI ran a two-line calculation on a table called loans. That table has 6 rows (L1 through L6), where each row is one book loan with four columns: loan_id, branch (North or South), member (m1 through m4), and returned_late (True or False). Notice that members m1 and m2 each appear in two rows because each of them borrowed two books.

Try it

Commit your prediction for each step of the AI's code below, run it to compare, and explain what happened in your own words. If the output surprises you on either step, ask your Maestro to walk through the rows with you.

Six loans, one row each. Predict what each program prints.

The filter

Example 1 of 2

example.py
import pandas as pdloans = pd.DataFrame({    "loan_id": ["L1", "L2", "L3", "L4", "L5", "L6"],    "branch": ["North", "North", "South", "South", "South", "North"],    "member": ["m1", "m2", "m1", "m3", "m4", "m2"],    "returned_late": [False, True, True, False, True, False],})late = loans[loans["returned_late"]]print(late)

A guess is fine. What you write stays in this browser and waits in your course.

Example 2 of 2, opens once you explain this one

A good place to stop. Up next: Read the calculation line by line · 3 min

Read the calculation line by line

The AI's calculation does all of its work in two lines at the bottom of the script. Read them from the inside out:

late_returns.pylines 11, 14
import pandas as pdloans = pd.DataFrame({    "loan_id": ["L1", "L2", "L3", "L4", "L5", "L6"],    "branch": ["North", "North", "South", "South", "South", "North"],    "member": ["m1", "m2", "m1", "m3", "m4", "m2"],    "returned_late": [False, True, True, False, True, False],})# Step 1: keep only the rows where returned_late is TrueStart of lines 11, 14. late = loans[loans["returned_late"]] End of lines 11, 14.# Step 2: split those rows by branch and count the rows in each groupStart of lines 11, 14. print(late.groupby("branch").size()) End of lines 11, 14.
Lines 11 and 14 filter the 6 loan rows down to 3 late rows, then group those 3 rows by branch and count them.

Here is what each piece computes:

  1. Filtering with a boolean column (late = loans[loans["returned_late"]]). When you filter by text or numbers, you write a comparison such as loans["branch"] == "North" to get a column of True and False values. Here, loans["returned_late"] already holds True and False for every row: [False, True, True, False, True, False]. Passing that column directly inside the outer brackets loans[...] keeps the rows where the value is True and drops the rest, without needing == True at the end. If loans[loans["returned_late"]] looks unusual without == True, ask your Maestro how the inner and outer brackets work together. The result is stored in late, a new 3-row table containing loans L2, L3, and L5. The original 6-row loans table stays unchanged.
  2. Splitting into groups (late.groupby("branch")). Calling .groupby("branch") on late sorts those 3 surviving rows into one bucket per branch value. North gets 1 row (L2), and South gets 2 rows (L3 and L5). Grouping leaves both loans and late intact; it only arranges the rows of late so the next function runs separately on each branch.
  3. Counting rows per group (.size()). Adding .size() counts how many rows sit in each branch bucket. It pays no attention to the member column, so it never checks whether two rows belong to the same borrower, and the table has no column for how many days overdue a book was. If you want to compare .size() with counting distinct borrowers, ask your Maestro for a side-by-side example. Because each row in late is one late loan, .size() returns the number of late loans per branch: 1 in North and 2 in South.

Check yourself: In your own words, trace what late = loans[loans["returned_late"]] and late.groupby("branch").size() did, and state what the final numbers (North 1, South 2) count in each branch: loans, members, or late days.

Check yourself: In your own words, trace what late = loans[loans["returned_late"]] and late.groupby("branch").size() did, and state what the final numbers (North 1, South 2) count in each branch: loans, members, or late days.

Ctrl+Enter or ⌘+Enter sends it.

Answer it yourself