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.
M19C01 · Explore · predict · practice
Melt wide columns into rows
A wide table stores different measures in different columns. melt turns selected measure columns into a label column and a value column. The ID is repeated so every new row still belongs to a student.
Four students with two measures produce eight rows.
Compare your explanations
Read. Student_ID.
Predict. 6 rows.
Explain. No. melt changes the layout; the values remain separate and their measure labels are retained.
One quick check
What does melt mainly change?
What you should be able to explain. Long form keeps one measured value per row.
M19C02 · Explore · predict · practice
Pivot a unique pair
pivot moves distinct category labels into columns. Every index-and-column pair must identify at most one value. Here each student has one Score and one Attendance value.
Read the example
long = students().head(3).melt(id_vars="Student_ID", value_vars=["Score", "Attendance"], var_name="Measure", value_name="Value")
result = long.pivot(index="Student_ID", columns="Measure", values="Value")
print(result)
Watch for this. Use pivot_table only when you actually intend to aggregate repeated pairs.
Your turn
Pivot the same long table so Measure labels form the rows and Student_ID labels form the columns.
For the code check, keep these result variable names: result. Different valid approaches are welcome.
Show a hint
Exchange the arguments assigned to index and columns.
Show one worked solution
long = students().head(3).melt(id_vars="Student_ID", value_vars=["Score", "Attendance"], var_name="Measure", value_name="Value")
result = long.pivot(index="Measure", columns="Student_ID", values="Value")
print(result)
The values stay the same, but the two table axes exchange roles.
Compare your explanations
Read. The distinct Measure values: Attendance and Score.
Predict. 72, unchanged.
Explain. pivot cannot choose which of several values belongs in the same output cell.
One quick check
What must be unique for pivot?
What you should be able to explain. pivot rearranges uniquely identified values.
M19C03 · Explore · predict · practice
Aggregate with pivot_table
Several students share the same Campus and Program. pivot_table can reduce those repeated combinations using an explicit aggregation function. Naming aggfunc makes the summary rule clear.
The counts are Dubai AI 2, Dubai IT 4, RAK AI 4, and RAK IT 2.
Compare your explanations
Read. The mean Score of students with that Campus and Program.
Predict. Four: two campuses times two programs.
Explain. It documents the statistic; mean, sum, and count answer different questions.
One quick check
Which method handles repeated pairs by summarizing them?
What you should be able to explain. Choose an aggregation that matches the question.
M19C04 · Explore · predict · practice
Count combinations with crosstab
crosstab counts how often two categories occur together. normalize="index" instead reports proportions within each row. The denominator changes, so always label the result.
Read the example
df = students()
result = pd.crosstab(df["Campus"], df["Program"])
print(result)
Verified example output · pandas 2.2.3
Program AI IT
Campus
Dubai 2 4
RAK 4 2
Watch for this. Say whether percentages are by row, by column, or of the whole table.
Your turn
Calculate program percentages within each campus and keep the unrounded percentages in result. Print a version rounded to one decimal place.
For the code check, keep these result variable names: result. Different valid approaches are welcome.