Recall & Review
beginner
What does the
sort_values() function do in pandas?It sorts the rows of a DataFrame based on the values in one or more columns.
Click to reveal answer
beginner
How do you sort a DataFrame by a single column named 'age' in ascending order?
Use
df.sort_values('age'). This sorts the DataFrame rows by the 'age' column from smallest to largest.Click to reveal answer
beginner
What parameter do you use to sort a column in descending order with
sort_values()?Set
ascending=False inside sort_values() to sort in descending order.Click to reveal answer
intermediate
What happens if you use
inplace=True with sort_values()?The DataFrame is sorted in place, meaning the original DataFrame changes and nothing is returned.
Click to reveal answer
intermediate
Why might you want to reset the index after sorting a DataFrame?
Because sorting changes the row order but keeps original indexes, resetting the index gives a clean, ordered index.
Click to reveal answer
Which code sorts a DataFrame
df by the column 'score' in ascending order?✗ Incorrect
The correct syntax is
df.sort_values('score') to sort ascending by 'score'.How do you sort a DataFrame by column 'height' in descending order?
✗ Incorrect
Use
ascending=False to sort descending with sort_values().What does
inplace=True do in sort_values()?✗ Incorrect
With
inplace=True, the original DataFrame is sorted and the function returns None.After sorting a DataFrame, why might you call
reset_index(drop=True)?✗ Incorrect
Sorting keeps original indexes; resetting index cleans them to be sequential.
Which of these is NOT a valid way to specify the column to sort by in
sort_values()?✗ Incorrect
The parameter name is
by, not columns.Explain how to sort a pandas DataFrame by a single column in ascending and descending order.
Think about the parameters of sort_values()
You got /4 concepts.
Describe why resetting the index after sorting a DataFrame might be useful.
Consider how DataFrame indexes behave after sorting
You got /3 concepts.