Article
Data sets, tables and spreadsheets
Your phone's contacts app holds hundreds of people. Each one has a name, a number and a city. The phone can find one name, sort them all, or count how many live in your city. How does it keep so much in order? It stores the contacts as a table.
Rows and columns
A data set is a collection of data about a group of things. Most data sets are stored as a table. Here is one about four students and their steps today:
| name | age | city | steps_today |
|---|---|---|---|
| Aisha | 14 | Dubai | 8200 |
| Ben | 15 | Leeds | 6400 |
| Chen | 14 | Dubai | 9100 |
| Dara | 16 | Lagos | 7300 |
- Each row is one record: everything the table knows about one thing. Here, one student.
- Each column is one field: one kind of fact, kept for every record. Here,
ageis a field. - Where a row and a column meet is one value, such as
Leeds.
The table has 4 records and 4 fields. The first row, with the field names, is a heading and is not a record. Every record has the same fields, in the same order. That is what makes a table easy for a program to read.
Check your understanding
Here is a table about pets.
| pet | kind | age |
|---|---|---|
| Milo | dog | 3 |
| Kiki | cat | 5 |
| Bobo | rabbit | 2 |
| Nina | cat | 4 |
| Rex | dog | 6 |
How many records and how many fields does the table have?
Spreadsheets and CSV files
A spreadsheet (such as Excel or Google Sheets) shows a data set as a table and lets you sort it, add up a column and draw a chart.
To store a table in a plain file, people often use CSV (comma-separated values). A CSV file has one line for each record. The values on a line are separated by commas. The first line holds the field names.
name,age,city,steps_today
Aisha,14,Dubai,8200
Ben,15,Leeds,6400
Chen,14,Dubai,9100
Dara,16,Lagos,7300A file holds only text, so 15 arrives as the characters 1 and 5. A program must turn it into a number before it can do sums. Run this code to see a program read the file. You don't need to change it, but you can add a fifth line to text.
A table in code: a list of lists
In a program, one record is a list, and the whole data set is a list of records. You met lists in Unit 3. A list can hold other lists.
people ← [["Aisha", 14, "Dubai", 8200], ["Ben", 15, "Leeds", 6400], ["Chen", 14, "Dubai", 9100], ["Dara", 16, "Lagos", 7300]] row ← people[2] DISPLAY(row[1]) DISPLAY(row[4])
people[2] is the second record, Ben's. In that record, row[1] is the name and row[4] is steps_today. The program displays Ben 6400. To add up one column, loop over the records:
total ← 0
FOR EACH row IN people
{
total ← total + row[4]
}
DISPLAY(total)row | row[4] | total |
|---|---|---|
| start | 0 | |
| Aisha's record | 8200 | 8200 |
| Ben's record | 6400 | 14600 |
| Chen's record | 9100 | 23700 |
| Dara's record | 7300 | 31000 |
It displays 31000. The program has no field names. It only knows positions, so you must remember that position 4 means steps_today. In Python the positions start at 0, so the same field is row[3]. The code you ran above shows people[1], and that is Ben's record too.
Check your understanding
What is displayed when this code runs?
books ← [["Atlas", 320], ["Poems", 90], ["Maps", 150]] row ← books[2] DISPLAY(row[1])
What can a table tell you?
A table can answer only questions about what is in it. This matters a lot on the exam. Look at the fields before you answer. Here are five questions about the table above:
| Question | Can the table answer it? |
|---|---|
| Who walked the most today? | Yes: Chen, with 9100 |
| How many students live in Dubai? | Yes: 2 |
| What is the mean age? | Yes: (14 + 15 + 14 + 16) / 4 = 14.75 |
| Who walked the most this week? | No: the table has only today's steps |
| Who is the tallest? | No: there is no height field |
The whole unit on one page
This unit fits on one map. Come back to it to see where a lesson belongs.
Data tools
- tables, records and fields
- statistics
- filter, sort and chart
- correlation is not causation
- metadata
- cleaning data
Big data
- data too big for one computer
- where it comes from
- the challenges
Bias in machine learning
- how a computer learns from examples
- unfair results from biased data
- faces and translation
Common mistakes
- Mixing up rows and columns. A row is one record. A column is one field.
- Counting the heading row as a record. The table above has 4 records, not 5.
- Answering about data the table does not have. Today's steps cannot tell you about the week.
- Forgetting that a CSV value is text.
"15"and15are not the same to a program. - Mixing up AP and Python positions. In AP, the first value of a record is
row[1]. In Python it isrow[0].
On the exam
You are shown a small table and asked "Which of the following can be determined using only this data?" or "Which question can not be answered from this data?" Name the fields first. Then check each answer against them. An answer that needs a field the table lacks is wrong.
Key words
Data set: a collection of data about a group of things.
Record: one row of a table: all the data about one thing.
Field: one column of a table: one kind of fact, kept for every record.
Spreadsheet: a program that shows data as a table and does calculations on it.
CSV: a text file with one line per record and commas between the values.