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.
M07C01 · Explore · predict · practice
Sort rows by a value
sort_values() orders whole rows by one or more column values. ascending=False puts larger values first. It returns a new table, so assign the result if you want to keep the order. The index labels travel with their rows and are not automatically renumbered.
Student_ID Name Score
5 S006 Fatima 95
2 S003 Chloe 91
9 S010 Jamal 89
Index labels: [5, 2, 9]
Watch for this. head(3) before sorting chooses the first three source rows, not the three highest scores.
Your turn
Find the three lowest scores instead. Keep Student_ID, Name, and Score in result.
For the code check, keep these result variable names: result. Different valid approaches are welcome.
Show a hint
Sort ascending and then take head(3).
Show one worked solution
df = students()
result = df.sort_values("Score", ascending=True).head(3)[["Student_ID", "Name", "Score"]]
print(result)
The three lowest scores are Hassan 58, Dana 65, and Khalid 68. Sorting whole rows keeps each score matched with its student.
Compare your explanations
Read. Descending order, from larger scores to smaller scores.
Predict. Fatima with 95, Chloe with 91, and Jamal with 89.
Explain. The original row labels stay attached to the records when the rows are sorted.
One quick check
Does sort_values rearrange only the selected column's values?
What you should be able to explain. I can sort a table and identify the top or bottom records.
M07C02 · Explore · predict · practice
Specify a tie rule
Rows can tie on a sort column. With one column, kind="stable" preserves their existing order within a tie. To define a new tie rule, sort by several columns and give one ascending flag per column. Clear tie rules make results easier to reproduce and explain.
Bilal remains first with 95. The three scores of 80 are then ordered Amira, Chloe, Dana.
Compare your explanations
Read. Chloe, Amira, and Dana all have 80.
Predict. Chloe, Amira, then Dana, preserving their order in the original table.
Explain. It provides a clear order among equal values, such as alphabetical order among students with the same score.
One quick check
What does a stable sort preserve for equal sort values?
What you should be able to explain. I can make the order of tied records explicit.
M07C03 · Explore · predict · practice
Set, check, and reset an index
An index labels rows; it is not automatically a unique ID. set_index() chooses the labels, and index.is_unique tells you whether any label repeats. reset_index() moves the current index back into a column and supplies new default row labels. Use reset_index(drop=True) only when you deliberately want to discard the current labels.
The selected rows are Chloe and Amira. Their IDs remain in the Student_ID column, and their new row labels are 0 and 1.
Compare your explanations
Read. It is True when no index label repeats. Separate checks are still needed for missing or invalid IDs.
Predict. Yes. It is restored as a column, while the row index becomes 0 through 11.
Explain. Student_ID is currently the index, so drop=True would discard those IDs instead of restoring them to a column.
One quick check
Which operation keeps the old index as a column?
What you should be able to explain. I can change an index without accidentally discarding record IDs.
M07C04 · Explore · predict · practice
Reindex to a requested order
reindex() aligns a table to a specified set of labels and their requested order. Existing labels bring their rows with them; new labels create missing values. This differs from sorting because you supply the desired labels explicitly. Use isna() to inspect newly missing cells.