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 multiple columns using
sort_values()?Pass a list of column names to the
by parameter, like df.sort_values(by=['col1', 'col2']).Click to reveal answer
intermediate
How can you sort one column ascending and another descending using
sort_values()?Use the
ascending parameter with a list of booleans, e.g., df.sort_values(by=['col1', 'col2'], ascending=[True, False]).Click to reveal answer
beginner
What happens if you don't specify the
ascending parameter in sort_values()?All columns are sorted in ascending order by default.
Click to reveal answer
intermediate
Can
sort_values() sort by columns with missing values (NaNs)?Yes, NaNs are placed at the end by default when sorting ascending, and at the beginning when sorting descending.
Click to reveal answer
How do you sort a DataFrame
df by columns 'A' ascending and 'B' descending?✗ Incorrect
You specify the columns in order and set ascending to True for 'A' and False for 'B'.
What is the default sort order if you do not specify
ascending?✗ Incorrect
By default,
sort_values() sorts all columns in ascending order.Which parameter do you use to specify the columns to sort by in
sort_values()?✗ Incorrect
The
by parameter takes a column name or list of column names to sort by.If you want to sort by a single column 'X' in descending order, which is correct?
✗ Incorrect
Set ascending=False to sort descending for a single column.
How does
sort_values() handle NaN values when sorting ascending?✗ Incorrect
By default, NaNs are placed at the end when sorting ascending.
Explain how to sort a pandas DataFrame by multiple columns with different sort orders.
Think about how you tell pandas which columns and order to use.
You got /4 concepts.
Describe what happens to missing values (NaNs) when sorting a DataFrame with sort_values().
Consider where NaNs appear in the sorted result.
You got /4 concepts.