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 Glossary word of a table tells you what one row represents. Your next step is reading the Glossary word 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.
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:
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.Here is what each piece computes:
- Filtering with a boolean column (
late = loans[loans["returned_late"]]). When you filter by text or numbers, you write a comparison such asloans["branch"] == "North"to get a column ofTrueandFalsevalues. Here,loans["returned_late"]already holdsTrueandFalsefor every row:[False, True, True, False, True, False]. Passing that column directly inside the outer bracketsloans[...]keeps the rows where the value isTrueand drops the rest, without needing== Trueat the end. Ifloans[loans["returned_late"]]looks unusual without== True, ask your Maestro how the inner and outer brackets work together. The result is stored inlate, a new 3-row table containing loansL2,L3, andL5. The original 6-rowloanstable stays unchanged. - Splitting into groups (
late.groupby("branch")). Calling.groupby("branch")onlatesorts those 3 surviving rows into one bucket per branch value.Northgets 1 row (L2), andSouthgets 2 rows (L3andL5). Grouping leaves bothloansandlateintact; it only arranges the rows oflateso the next function runs separately on each branch. - Counting rows per group (
.size()). Adding.size()counts how many rows sit in each branch bucket. It pays no attention to themembercolumn, 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 inlateis one late loan,.size()returns the number of late loans per branch: 1 inNorthand 2 inSouth.