Recall & Review
beginner
What does the pandas method
sort_values() do?It sorts a DataFrame or Series by the values in one or more columns or the Series itself, arranging data in ascending or descending order.
Click to reveal answer
beginner
How do you sort a DataFrame by a single column named
'age' in ascending order?Use
df.sort_values(by='age'). This sorts the DataFrame rows so that the smallest age is first and the largest last.Click to reveal answer
intermediate
How can you sort a DataFrame by multiple columns, for example
'age' ascending and 'score' descending?Use
df.sort_values(by=['age', 'score'], ascending=[True, False]). This sorts first by age ascending, then by score descending within each age.Click to reveal answer
beginner
What does the parameter
inplace=True do in sort_values()?It changes the original DataFrame directly without creating a new sorted copy. If
inplace=False (default), it returns a new sorted DataFrame.Click to reveal answer
beginner
How do you sort a pandas Series in descending order?
Use
series.sort_values(ascending=False). This arranges the Series values from largest to smallest.Click to reveal answer
Which pandas method sorts a DataFrame by column values?
✗ Incorrect
sort_values() sorts data by column or Series values. sort_index() sorts by index labels.
How do you sort a DataFrame
df by column 'score' in descending order?✗ Incorrect
Set ascending=False to sort from highest to lowest values.
What happens if you use
inplace=True in sort_values()?✗ Incorrect
inplace=True sorts the original DataFrame and returns nothing (None).
How do you sort a DataFrame by two columns,
'age' ascending and 'score' descending?✗ Incorrect
Use a list for by and a matching list of booleans for ascending to control order per column.
Which of these is NOT a valid argument for
sort_values()?✗ Incorrect
groupby is a separate pandas method, not an argument for sort_values().
Explain how to sort a pandas DataFrame by one or more columns with different ascending/descending orders.
Think about how to control sorting order for each column separately.
You got /5 concepts.
Describe the effect of the 'inplace' parameter in the sort_values() method.
Consider whether the original data changes or a copy is made.
You got /4 concepts.