Join and check the tables
Part 3 · TODO 2 and merge checks
many_to_one is the lecture's key rule in code. Unique student keys on the right prevent registrations from multiplying.
Week 1: joins, PDF pp. 42–53; Week 3 inventory is a later connection
Your task
Predict the joined row count. Complete TODO 2, run the merge and inspect both assertions.
Evidence to include
Give before/after row counts, the final row meaning, and what duplicate right-side keys could cause.
Notebook questions and code
Read here; run in your downloaded notebook. Start with its setup cells and run in order. Use your own ID and outputs where requested.
Download Lab 1 notebookPart 3: Connect the tables
We want one row per course registration, plus the student's programme and study mode.
- enrolments stays on the left because it already has one row per registration.
- students supplies extra student information.
- many_to_one means many enrolment rows can match one student row.
Predict: The merge should have the same number of rows as enrolments. Why?
Python · cell 13 — read here, run in your notebook
# TODO 2 - Complete the merge by replacing the blank with one_student_table.
# Do not change on, how, or validate.
joined = enrolments.merge(
_____[["student_id", "programme", "study_mode"]],
on="student_id",
how="left",
validate="many_to_one",
)
joined.head()Python · cell 14 — read here, run in your notebook
print("Rows before merge:", len(enrolments))
print("Rows after merge: ", len(joined))
assert len(joined) == len(enrolments), "Unexpected row change: stop and ask for help."
assert joined["programme"].notna().all(), "A student did not match: stop and ask for help."
print("Check passed: one row still represents one student-course registration.")Explain after connecting
- After the merge, what does one row in joined represent?
- Why does the row count stay the same?
- What would it mean if the merge created twice as many rows?
- Why is many_to_one appropriate here?
Answer each in one short sentence.
Saved only in this browser. This note doesn’t fill in the downloaded notebook; “reviewed” is a personal checklist, not a grade.