Asking a table a question
Problem 21 told you what was in the file. This one starts using it: work out a new column from two existing ones, keep only the columns worth looking at, and keep only the rows that matter.
df["percent"] = df["classes_attended"] / df["classes_held"] * 100Two whole columns divided by each other, row by row, exactly as in problem 18 — no loop. Assigning to a column name that does not exist yet creates it, the same way writing to a new key created one in the dictionary in problem 8.
df["name"] is one column as a Series.df[["name", "percent"]] is a list of column names inside the brackets, and what comes back is a table with those columns in that order.The inner brackets are the list. That is why picking several columns needs two pairs and picking one needs only one.
df["percent"] < 80 gives one True or False per row, exactly like the NumPy mask in problem 19.Put it back inside the square brackets —
df[df["percent"] < 80] — and you get the whole rows where it was True. Each student's name and city comes along with their attendance, because rows stay together. That is the thing a table does that three separate lists never could.Notice the index numbers that come back: 1, 3, 5. They are the original row numbers, not a fresh count, so you can always tell where a row came from.
df.loc[rows, columns] asks for both at once: which rows, and which columns of them.df.loc[df["percent"] < 80, "name"] reads as the names of the students below 80. The mask on the left chooses rows; the name on the right chooses what to show. It is the same selection you could do in two steps, said in one.What you are building
Build the programme
Your plan
The steps still to place
The whole programme is laid out below. Five pieces are missing. Only one of them can stop the programme; the other twelve wrong choices all hand you a tidy, plausible, wrong answer.
Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.
Run and read
Here is the whole programme, reading the tidy file from problem 21. The cutoff is yours to change — try 60, where only one student falls below, and 100, where nearly everybody does.