Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.
M25C01 · Explore · predict · practice
A label is not a row position
A table can use student IDs as its index. Position 0 still means the first row, but the label of that row is now S001. Read the index before choosing a selector.
Both results contain Chloe, Dana, and Ethan. Labels and positions happen to select the same records here, but they express different rules.
Compare your explanations
Read. The Student_ID values, such as S001, are index labels.
Predict. iloc excludes stop position 2; loc includes both existing endpoint labels S001 and S002.
Explain. No. It asks for label 0, which is absent, so it raises KeyError.
One quick check
Which stop is excluded?
What you should be able to explain. Choose a selector from the meaning of the request, not from habit.
M25C02 · Explore · predict · practice
Repair a condition with two rules
A filter must produce one True or False value per row. Combine pandas conditions with & for AND or | for OR, with parentheses around each comparison. The broken expression is a comment so this example can run.
S004, S008, and S011 meet at least one condition. S011 qualifies by score even though attendance is 82.
Compare your explanations
Read. A student meeting both the score and attendance conditions.
Predict. Yes. Her score is 82 and attendance is 94.
Explain. and expects a single truth value. Each comparison returns a Series of truth values, which & combines element by element.
One quick check
A student meets only one of two conditions. Which operator keeps that student?
What you should be able to explain. Translate AND, OR, and NOT into &, |, and ~.
M25C03 · Explore · predict · practice
Change the table you actually intend to change
A filtered table and the original table are different objects. Use a single .loc assignment when the original must change. Use .copy() when creating a separate working table, and inspect both afterward.
Watch for this. A method returning a new object is not an instruction to update the old one.
Your turn
Start with a fresh df = students() and create a separate copy of its RAK rows named rak. Add 5 points to scores in rak. Show that Amira's copied score is 93 while her score in df remains 88.
For the code check, keep these result variable names: df, rak. Different valid approaches are welcome.
The working copy changes; the original data stays available for comparison. For these numeric columns, .copy() makes the intended separation explicit.
Compare your explanations
Read. df itself, through one .loc operation.
Predict. 70. The original example value was 65.
Explain. It writes through an intermediate object. Under pandas Copy-on-Write it cannot update the original DataFrame.
One quick check
Which expression clearly updates df?
What you should be able to explain. Make every data change explicit.
M25C04 · Explore · predict · practice
A correct total can still hide a bad merge
Two tables may contain the same key more than once. Matching repeated keys can multiply records. Validate the intended relationship and inspect unmatched IDs before calculating a report.
Read the example
left = students()[["Student_ID", "Name"]]
right = assessment()
result = left.merge(right, on="Student_ID", how="outer",
validate="one_to_one", indicator=True)
print(result.loc[result["_merge"].ne("both"), ["Student_ID", "_merge"]])
print("Rows:", len(result))
Watch for this. Do not remove duplicate keys without understanding why they exist.
Your turn
Keep only enrolled students with a left merge stored in result. Keep the merge indicator column, and store the count of students with no assessment in missing_count.
For the code check, keep these result variable names: missing_count, result. Different valid approaches are welcome.
Show a hint
Use how='left', validate='one_to_one', and indicator=True.
Show one worked solution
result = students().merge(assessment(), on="Student_ID", how="left",
validate="one_to_one", indicator=True)
missing_count = result["_merge"].eq("left_only").sum()
print(result[["Student_ID", "Quiz", "_merge"]])
print("Enrolled students without assessment:", missing_count)
The result has 12 enrolled students and one missing assessment, S012. The earlier outer audit also exposed S013 for follow-up.
Compare your explanations
Read. The merge keys must be unique in each input table.
Predict. S012 appears only in students; S013 appears only in assessment.
Explain. It preserves left-side students but discards right-only records, including S013.
One quick check
What can a many-to-many merge do?
What you should be able to explain. Audit the join before trusting the summary.