← Data detective

Data detective: recap

For after the lessons: one card per lesson, to check what you remember. Answer each question in your head or on paper first, then check it. Nothing here is marked.

Reading data

1. Rows of data

Try firstWhy does int(song["year"]) need int()?

Check your answer

Every value read from a CSV is text; int() turns "2013" into the number 2013 so it can be compared or added.

  1. csv.DictReader reads the file's first line as the keys, and turns every other line into a dictionary.
  2. songs[0] is the first row; songs[0]["title"] is one value in it.
  3. Every value is text, even the year. int() makes it a number before you compare or add.
csv.DictReader turns each row of the file into a dictionary · Sound on for the voiceover
Open the lesson →

2. Totals and the biggest

Try firstWhy does an average of the pop songs need its own count?

Check your answer

len(songs) counts every song; the average of some songs divides their total by how many of them there are, counted inside the if.

  1. A total starts at 0, and each row adds to it: total = total + minutes.
  2. The average is the total divided by how many rows were added.
  3. The longest so far starts as the first song and is replaced only by a longer one.
Open the lesson →

3. The same job in pandas

Try firstWhat does songs[songs["year"] < 1990] do?

Check your answer

It keeps only the rows where the year is below 1990, and gives them back as a smaller table.

pd.read_csv("file.csv") reads a CSV into a table. table["column"] is one column, and .mean(), .max() and .min() summarise it. len(table) is the number of rows. table[test] keeps the rows where the test is True.

Open the lesson →

Charts and checking

4. A bar chart in text

Try firstWhy does counting need if genre in counts:?

Check your answer

The first time a genre appears it has no count yet, so counts[genre] + 1 would fail; it starts at 1 instead.

  1. counts starts as an empty dictionary.
  2. Each song adds 1 to its genre's count, or starts that count at 1 if the genre is new.
  3. A second loop draws a bar for each genre: "#" times its count.
A dictionary keeps one count per genre; each count becomes a bar · Sound on for the voiceover
Open the lesson →

5. Clean it, test it

Try firstWhat does a test case such as assert clean_hours("400") is None prove?

Check your answer

That the function rejects an impossible answer; if someone breaks the function later, the program stops on that line and says so.

  1. clean_hours gives back a number, or None when an answer cannot be used: not a number, or impossible.
  2. assert lines are test cases: silent when true, and they stop the program when false.
  3. The loop skips the rows that come back None, and the result says how many answers it used.
Open the lesson →

Dashboards and live data

6. A data dashboard

Try firstHow does Gradio know what to pass to your function?

Check your answer

It calls the function with one value for each input, in the order the inputs are listed, and shows each returned value in the outputs, in order.

  1. pd.read_csv reads the file into a table; numbers stay numbers.
  2. songs[songs["genre"] == genre] keeps only the rows for the chosen genre.
  3. Gradio calls the function with the dropdown's choice, and shows what it returns: a table and a bar chart.
Choose a genre: the function filters the table, and Gradio shows the result · Sound on for the voiceover
Open the lesson →