Unit 5 · Data · Lesson 5.7

Filtering and Grouping

Two moves that turn a pile of rows into an answer. Filter: keep only the rows that match a condition. Group: one count (or total) per category. Both are Unit 2–3 tools, combined.

Big Idea 2 — DataCED 2.4 · AP item: loop + IF over a list

Before this lesson — read on Runestone

Watch how the code picks out rows that meet a condition — that’s filtering. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Big Data. 5.6 cleaned the data. Today you slice it: one subset, one category at a time. The exam’s most common data item — “what does this loop with an IF compute?” — is this lesson at small scale.

2

The Hook

“Pizza is the most popular lunch.” True for the whole school. Is it true for 12th graders? You can’t tell from one number. You have to filter to 12th graders, then group by lunch choice — and the answer might flip.
3

The Idea

Filter = selection (Unit 2) inside traversal (Unit 3). Keep a row only if it passes a test. Aggregate = combine many values into one: a count, a total, an average, a max. Grouping = one aggregate per category.

Python

def count_where(rows, field, value):
    count = 0
    for row in rows:
        if row[field] == value:
            count = count + 1
    return count
 
for food in ["Pizza", "Salad", "Burrito"]:
    print(food, count_where(rows, "choice", food))

AP Pseudocode

PROCEDURE countWhere(list, value)
{ count ← 0
  FOR EACH item IN list
  { IF (item = value)
    { count ← count + 1 } }
  RETURN(count) }
 
FOR EACH food IN ["Pizza", "Salad", "Burrito"]
{ DISPLAY(countWhere(choices, food)) }

Hover or tap a line to light its twin. The parameter is the twist. One procedure, called once per category, gives the whole grouped count — and a procedure with a parameter is exactly what the Create PT asks for.

Filter, then aggregate. To answer “most popular lunch among 12th graders,” build the subset first:

seniors = []
for row in rows:
    if row["grade"] == "12":        # still a string — it came from a CSV
        seniors.append(row)
for food in ["Pizza", "Salad", "Burrito", "Pasta"]:
    print(food, count_where(seniors, "choice", food))
Faster way, later. Python’s dictionaries can build every count in a single pass. You don’t need them — and the AP exam never uses them. The procedure above is the exam’s way.
4

Try It — Filter, Then Group

18 lunch orders. Pick a filter, then step through the rows and watch each tally grow. Does the most popular choice change when you filter?

filter: grade =
5

Vocabulary

filter Big Data
Keeping only the records that meet a condition, producing a subset of the data.
aggregate Big Data
Combining many values into one summary value — a count, sum, average, max, or min. Grouping computes one aggregate per category.
6

Check

Five questions. AP lists start at index 1.

0 of 5 answered
7

Impact Check

The average that hides a group

An overall number can be true and still hide something important. A city where “average commute is 22 minutes” might have one neighborhood averaging 50. A school with a high pass rate might have one group far below it. Filtering and grouping are how you find out who the average is hiding — and they’re how fairness problems in data get caught.

8

Connections

Came from 2.12 + 3.8 + 3.12 — selection inside traversal, wrapped in a procedure with a parameter.
Next in Lab 5.8 — group the club data by grade, then chart it — honestly and dishonestly.
Returns in Mini-Project 4 — count_where or average_by is the required procedure.
Returns on the exam — “What is displayed?” with a loop and an IF. The single most common BI2 code item.
← 5.6 Dirty Data