DATA SCIENCE · CYCLE 2 · L22 · TEXTBOOK CHAPTER
Ask a Big File
Nobody can read a file of 36,574 rows. A big table answers when you ask it. You can keep only some rows, find one group’s mean, or get every group at once.
After this chapter, you can
- Keep only some rows with a filter.
- Find one group’s mean with =AVERAGEIF.
- Find every group’s mean and count with one =QUERY.
- Count the rows before and after, and log it.
- Say how volume, velocity and variety change how data is stored.
For your project: CLEAN, the group table and your first LOG row. The checkpoint is at the end.
Why It Matters
Capital Bikeshare publishes every trip its riders take, in a new file each month. One row is one trip: when it started, when it ended, the two stations, and whether the rider was a member or a casual rider.
The September 2025 file, for the 99 stations in Arlington and Alexandria alone, has 36,574 trips. At one row a second, reading it would take more than 10 hours. Nobody can scroll that. So you ask it.
Here is one ask: how many trips did each kind of rider take? The file answers in two numbers. Members took 24,293 trips, and casual riders took 12,281.
The Idea
Three ways to ask
You cannot scroll a big table, so you ask it. A question written for a table is a query. There are three ways to ask.
| The ask | In Sheets | You get |
| Keep only some rows | Data > Create a filter | the rows that match; the rest are hidden, never deleted |
| One group’s mean | =AVERAGEIF | one number |
| Every group at once | =QUERY with group by | a small table: a row for each group |
A filter shows only the rows that match; the rest are hidden. A group is the rows that share one label, like grade 9.
How to Read a QUERY
This is the QUERY from your task sheet. The part inside the quotes is a sentence in a small language of its own. Read it one part at a time.
=QUERY(DATA!A1:M13584,"select C, avg(J), count(J) where C is not null group by C",1)
| In a QUERY | It means |
DATA!A1:M13584 | the table: columns A to M of the DATA tab, the header row and every row under it |
select C | show column C |
avg(J), count(J) | the mean of column J, and how many numbers it has |
where C is not null | keep only the rows where column C is not empty |
group by C | one row for each value in column C |
,1 at the end | the table has 1 header row |
When an ask leaves rows out, count the rows before and after, and write one log row: what you did, why, and the two counts.
How a Big File Is Stored
Three things decide how data is stored.
| The word | It means | In the Bikeshare file |
| volume | how much data there is | 36,574 rows in one month |
| velocity | how fast new data arrives | more than 1,200 trips a day |
| variety | how many kinds of data there are | times, station names, map points and a label |
So the trips are stored as one new file a month, one row a trip. No one keeps them in one Sheet.
Worked Examples
A librarian wrote down eight visits: the kind of day, and how many books the visitor borrowed. The Sheet holds them in A1 to B9.
| A | B |
| 1 | day | books |
| 2 | school | 2 |
| 3 | weekend | 5 |
| 4 | school | 1 |
| 5 | school | 3 |
| 6 | weekend | 7 |
| 7 | school | 2 |
| 8 | weekend | 3 |
| 9 | school | 4 |
Example 1 · One group. How many books does a weekend visitor borrow, on average?
Create a strategyKeep only the weekend rows, then find their mean. A filter does the first part. =AVERAGEIF does both in one cell.
Apply the idea
- Click cell A1.
- Data > Create a filter.
- Click the filter icon in A1, day.
- Clear, tick weekend, OK.
Rows 3, 6 and 8 show: 5, 7 and 3 books. Their mean is 15 ÷ 3 = 5 books.
Now the same ask in one cell. Turn the filter off with Data > Remove filter. Click cell D1. Type: =AVERAGEIF(A2:A9,"weekend",B2:B9) It shows 5.
Reflect and checkRead the formula in English: the mean of column B, in the rows where column A says weekend. The filter hid five rows and deleted none. The formula never needed the filter: it reads every row, hidden or showing.
Example 2 · Every group at once. What is the mean for each kind of day, and how many visits is each mean built on?
Create a strategyOne =QUERY with group by makes a row for each kind of day.
Apply the idea
Click cell D3. Type: =QUERY(A1:B9,"select A, avg(B), count(B) group by A",1)
| day | avg books | count books |
| school | 2.4 | 5 |
| weekend | 5 | 3 |
School days: 2 + 1 + 3 + 2 + 4 = 12, and 12 ÷ 5 = 2.4 books. Weekend days: 15 ÷ 3 = 5 books.
Reflect and checkThe weekend row says 5, the same as Example 1: two formulas, one number. Now count. The Sheet has 8 rows of visits, and the QUERY counted 5 + 3 = 8. No row was left out.
Example 3 · Count before and after. The librarian wants only the visits with more than 2 books. What changes?
Create a strategyAdd a where part to keep only those rows. Then count the rows before and after, and write one log row.
Apply the idea
=QUERY(A1:B9,"select A, avg(B), count(B) where B > 2 group by A",1)
| day | avg books | count books |
| school | 3.5 | 2 |
| weekend | 5 | 3 |
Only two school rows have more than 2 books: 3 and 4, with a mean of 3.5. All three weekend rows stay.
| What I did | Why | Count before | Count after |
| Left out visits with 2 books or fewer | The question is about bigger loans | 8 | 5 |
Reflect and checkThe school mean jumped from 2.4 to 3.5, and nothing on the screen said why. The two counts do: 8 rows went in and 5 were used. Anyone who reads the log row can see that 3 rows were left out, and check.
The Same Three Asks in Python
These are the three cells Mr. Case runs on the board, on the class file: 13,583 students, one row each. They run on this page too. Press ▶ Run under each one, in order. Say your guess first. On this page the file is already here, so no Choose files button shows. One pandas word is new: groupby. Its twin is the QUERY’s group by.
1from google.colab import files
2import pandas as pd
3name = next(iter(files.upload()))
4df = pd.read_csv(name)
5print(len(df), "rows")
In Sheets this is: File > Import the CSV, then =COUNTA(DATA!A2:A13584), the count before.
In English:
- Get the tool that opens files.
- Get the pandas toolbox and call it pd.
- Show a Choose files button, and call the name of the file you pick name.
- Open the file as a table called df.
- Print how many rows.
Predict: one row for each student in the survey. How many rows will it say?
Can’t run it here? Show what it prints
13583 rows
1nine = df[df["grade"] == "9"]
2print(round(nine["physically_active_7d"].mean(), 2))
In Sheets this is: the filter that keeps grade 9, then =AVERAGEIF(DATA!C:C,9,DATA!J:J).
In English:
- Keep the rows where grade is 9, and call them nine.
- Print the mean of the physically_active_7d column of nine, rounded to 2 places.
Predict: cell A1 of your L22 tab holds this mean. What will Python print, to 2 places?
Can’t run it here? Show what it prints
4.11
The 9 is in quotes because the grade column also holds the word other, so pandas reads every grade as text.
In Sheets this is: =QUERY(DATA!A1:M13584,"select C, avg(J), count(J) where C is not null group by C",1): every grade at once.
In English:
- Split the rows into groups by grade, take the physically_active_7d column of each group, and call the result by_grade.
- Print the mean of each group, rounded to 2 places.
Predict: how many groups will it print, and in what order? Run it. Then click in the cell and change .mean() to .count(). Say what it will print, then run it.
Can’t run it here? Show what it prints
grade
10 4.04
11 3.83
12 3.64
9 4.11
other 3.91
Name: physically_active_7d, dtype: float64
With .count() it prints:
grade
10 3076
11 3110
12 3509
9 3515
other 23
Name: physically_active_7d, dtype: int64
Same means, a different table. Python lists the grades as text, so 10, 11 and 12 come before 9. It keeps a group named other, 23 students, that the QUERY leaves out: in Sheets the grade column is mostly numbers, so the QUERY treats a word as empty. The last line, Name and dtype, is the name of the column and its kind of number.
Now count. The file has 13,583 rows. The four grades’ counts add to 3,515 + 3,076 + 3,110 + 3,509 = 13,210. So 373 rows are in no grade’s mean: their grade says NA or other, or their days say NA. That is one log row: left out rows with no grade or no days active; 13,583 before, 13,210 after.
Want to type your own? The Python practice page has tasks and a notebook.
In your project
On your CLEAN tab, column A is your group and column B is your number. Your ANALYSIS tab then asks CLEAN the same way for every dataset: =COUNTIF(CLEAN!A:A,A7) counts a group’s rows, and =AVERAGEIF(CLEAN!A:A,A7,CLEAN!B:B) finds its mean. Your LOG tab gets its first row: what you did, why, the rows in RAW and the rows in CLEAN.
Check Yourself
Answer each one before you look. The first two are from L21 and L19b.
A quiz has mean 50 and SD 8. Find the z-score of a 38. L21
z = 38 − 508 = −128 = −1.5
Subtract the mean first, then divide by the SD. Negative: the score is below the mean.
What does line 2 print? L19b
1t = pd.Series([4, 9, 5])
2print(t.median())
In Sheets this is: =MEDIAN on three numbers. In English: 1. Make a column of the three numbers, and call it t. 2. Print the median of t.
In order: 4, 5, 9. The middle one is 5. pandas keeps every median as a decimal, so it prints 5.0.
Six trips to school are in a Sheet, in A1 to B7. What does =AVERAGEIF(A2:A7,"bus",B2:B7) show?
| A | B |
| 1 | way | minutes |
| 2 | bus | 30 |
| 3 | walk | 12 |
| 4 | bus | 40 |
| 5 | car | 15 |
| 6 | walk | 18 |
| 7 | bus | 20 |
The bus rows are 30, 40 and 20. 30 + 40 + 20 = 90, and 90 ÷ 3 = 30 minutes. 22.5 is the mean of all six rows: the formula uses only the rows that say bus.
The same Sheet. How many rows does =QUERY(A1:B7,"select A, avg(B) group by A",1) show under its header row?
One row for each value in column A: bus, car and walk. That is 3 rows. (6 is the rows in the Sheet. A QUERY with group by shows a row a group.)
Which word in a QUERY keeps only some rows?
where keeps the rows that pass a test, like where B > 2. select picks the columns to show. avg finds a mean.
A file has 2,400 rows. The counts in a QUERY’s table add to 2,310. How many rows were left out?
2,400 − 2,310 = 90 rows. Count before, count after: the difference is what the ask left out. It goes in the log row.
A filter keeps grade 12. What happened to the other rows?
A filter hides rows. Data > Remove filter brings every row back. Nothing is deleted and nothing moves.
Python printed the grades in this order: 10, 11, 12, 9. Why is 9 last?
The grade column also holds the word other, so pandas reads every grade as text and sorts letter by letter: “1” comes before “9”. Grade 9 has the most students and the highest mean of the four.
A traffic counter adds one new row every minute. Which word is that about?
Velocity is how fast new data arrives. Volume is how much there is. Variety is how many kinds there are.
Project Checkpoint
Homework 7 · due Tue 8 Dec
Every dataset now has the same two columns, group and number, so one formula compares your two groups. The group table is the first draft of the number your claim is about.
Download your CLEAN tab: click it, then File > Download > Comma-separated values (.csv). Then run this cell and choose that file. It is read on this computer and sent nowhere.
In Sheets this is: ANALYSIS B7 to C8: =COUNTIF(CLEAN!A:A,A7) and =AVERAGEIF(CLEAN!A:A,A7,CLEAN!B:B).
In English:
- Get the tool that opens files.
- Get the pandas toolbox and call it pd.
- Show a Choose file button, and call the name of the file you pick name.
- Open the file as a table called df.
- Keep only the numbers in the number column, and put them back in that column.
- Split the rows into groups by the group column, take the number column of each, and call the result by_group.
- Print how many numbers each group has.
- Print each group's mean, rounded to 2 places.
Predict: your means are in C7 and C8. Will Python's match? Python counts numbers, and =COUNTIF counts rows: a gap between them is the rows in that group with no number. Python also lists every group in column A, not only your two.
Tick each step once it is in your Sheet. Your ticks go into the PDF you hand in. Where everything goes: your Sheet, tab by tab.
Read More
Google, “QUERY function”, from the Google Docs Editors Help pages. Read Syntax and Sample Usage. It also says what QUERY does when one column mixes numbers and words: “Minority data types are considered null values.” That is why NA and other dropped out of the grade column.
Stuck on any of this? Ask Mr. Case in class or send him a message in Canvas. Copy the formula that lost you into the message.