L22 Ask a Big File

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

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 askIn SheetsYou get
Keep only some rowsData > Create a filterthe rows that match; the rest are hidden, never deleted
One group’s mean=AVERAGEIFone number
Every group at once=QUERY with group bya 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 QUERYIt means
DATA!A1:M13584the table: columns A to M of the DATA tab, the header row and every row under it
select Cshow column C
avg(J), count(J)the mean of column J, and how many numbers it has
where C is not nullkeep only the rows where column C is not empty
group by Cone row for each value in column C
,1 at the endthe 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 wordIt meansIn the Bikeshare file
volumehow much data there is36,574 rows in one month
velocityhow fast new data arrivesmore than 1,200 trips a day
varietyhow many kinds of data there aretimes, 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.

AB
1daybooks
2school2
3weekend5
4school1
5school3
6weekend7
7school2
8weekend3
9school4

Example 1 · One group. How many books does a weekend visitor borrow, on average?

Create a strategy

Keep only the weekend rows, then find their mean. A filter does the first part. =AVERAGEIF does both in one cell.

Apply the idea
  1. Click cell A1.
  2. Data > Create a filter.
  3. Click the filter icon in A1, day.
  4. 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 check

Read 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 strategy

One =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)

dayavg bookscount books
school2.45
weekend53

School days: 2 + 1 + 3 + 2 + 4 = 12, and 12 ÷ 5 = 2.4 books. Weekend days: 15 ÷ 3 = 5 books.

Reflect and check

The 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 strategy

Add 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)

dayavg bookscount books
school3.52
weekend53

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 didWhyCount beforeCount after
Left out visits with 2 books or fewerThe question is about bigger loans85
Reflect and check

The 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:

  1. Get the tool that opens files.
  2. Get the pandas toolbox and call it pd.
  3. Show a Choose files button, and call the name of the file you pick name.
  4. Open the file as a table called df.
  5. 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:

  1. Keep the rows where grade is 9, and call them nine.
  2. 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:

  1. Split the rows into groups by grade, take the physically_active_7d column of each group, and call the result by_grade.
  2. 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

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.

Six trips to school are in a Sheet, in A1 to B7. What does =AVERAGEIF(A2:A7,"bus",B2:B7) show?

AB
1wayminutes
2bus30
3walk12
4bus40
5car15
6walk18
7bus20

The same Sheet. How many rows does =QUERY(A1:B7,"select A, avg(B) group by A",1) show under its header row?

Which word in a QUERY keeps only some rows?

A file has 2,400 rows. The counts in a QUERY’s table add to 2,310. How many rows were left out?

A filter keeps grade 12. What happened to the other rows?

Python printed the grades in this order: 10, 11, 12, 9. Why is 9 last?

A traffic counter adds one new row every minute. Which word is that about?

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:

  1. Get the tool that opens files.
  2. Get the pandas toolbox and call it pd.
  3. Show a Choose file button, and call the name of the file you pick name.
  4. Open the file as a table called df.
  5. Keep only the numbers in the number column, and put them back in that column.
  6. Split the rows into groups by the group column, take the number column of each, and call the result by_group.
  7. Print how many numbers each group has.
  8. 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.