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.
M08C01 · Explore · predict · practice
Calculate a new column
Assign a Series to a new column name to add a field. Arithmetic across columns works row by row after index alignment. Here the artificial Score_Per_Hour ratio demonstrates division; it is not a validated measure of student ability. Always consider units and the meaning of a calculation.
Watch for this. Before dividing by a column, check for zero and missing denominators; they can create undefined results.
Your turn
Start with a fresh df = students(). Create Score_Fraction by dividing Score by 100. Store Name, Score, and Score_Fraction for all students in result, then print its first three rows.
For the code check, keep these result variable names: df, result. Different valid approaches are welcome.
Show a hint
Assign the division result to df["Score_Fraction"].
The first fractions are 0.88, 0.72, and 0.91. The original Score remains measured out of 100.
Compare your explanations
Read. df["Score_Per_Hour"] on the left side.
Predict. 11.0, from 88 divided by 8.
Explain. The ratio alone does not measure learning quality or account for other factors. It is only an arithmetic example with fictional data.
One quick check
What happens when you assign to a column name that does not yet exist?
What you should be able to explain. I can create a calculated column and state its units and limits.
M08C02 · Explore · predict · practice
Use assign, rename, and drop
assign() returns a DataFrame with new or replaced columns. rename(columns=...) changes column labels, while drop(columns=...) removes named columns. These methods return new objects by default. Assign the returned result so your intended changes are kept.
Needs_Support is True for Dana, Hassan, and Khalid. The longer attendance name makes the unit explicit. df remains available in its original form.
Compare your explanations
Read. True for scores at least 60 and False otherwise, using the rule stated in this example.
Predict. No. In result it has been renamed Student_Name. The original df still has Name.
Explain. It starts with nine, adds Passed, then drops Join_Date: 9 + 1 - 1 = 9.
One quick check
Which removes a named column from the returned table?
What you should be able to explain. I can add fields and tidy column structure while keeping the original available.
M08C03 · Explore · predict · practice
Update the original with one loc assignment
Use one .loc[row_condition, column_name] assignment to change selected values in a DataFrame. This explicitly identifies both the rows and the field being changed. Avoid chained assignment such as df[mask]["Score"] = value; it does not reliably update the intended original object and does not do so under pandas 3 Copy-on-Write.
Amira, Chloe, Fatima, and Jamal receive the practice bonus. Their scores become 91, 94, 98, and 92. Students outside the mask keep their scores.
Compare your explanations
Read. The Score column for the row whose Student_ID is S004, Dana.
Predict. 67.
Explain. It tells pandas to change the named column in selected rows of df itself, instead of writing into an intermediate selection.
One quick check
Which is the clear way to update selected scores in df?
What you should be able to explain. I can modify selected values using one explicit loc assignment.
M08C04 · Explore · predict · practice
Keep an independent working copy
Assigning original to another name creates an alias: both names refer to the same Python object. copy() creates a separate DataFrame for this ordinary scalar-valued teaching table. Use a fresh copy when you want to experiment without changing the original. Pandas copy rules do not replace the need to understand which object you are editing.
Read the example
original = students()
working = original.copy()
working.loc[0, "Score"] = 0
print("Original first score:", original.loc[0, "Score"])
print("Working first score:", working.loc[0, "Score"])
print("Same object:", working is original)
Verified example output · pandas 2.2.3
Original first score: 88
Working first score: 0
Same object: False
Watch for this. For columns containing nested mutable Python objects, even a deep DataFrame copy does not recursively copy those inner objects; this table uses ordinary scalar values.
Your turn
Copy only the RAK rows into working. Add 1 to their scores in the copy and verify Amira's original score is still 88.
For the code check, keep these result variable names: original, working. Different valid approaches are welcome.
Show a hint
Filter with loc, call .copy(), and update the selected table rather than original.
Show one worked solution
original = students()
working = original.loc[original["Campus"] == "RAK"].copy()
working["Score"] = working["Score"] + 1
print(working[["Name", "Score"]])
print("Amira in original:", original.loc[0, "Score"])
working contains six RAK students with scores increased by 1. Amira has 89 in working and 88 in original.
Compare your explanations
Read. Change values in working while keeping original unchanged.
Predict. 88 for original and 0 for working.
Explain. Both names would refer to the same object; changing working would also be visible through original.
One quick check
What does working = original do?
What you should be able to explain. I can protect source values while testing changes on a separate table.