Challenge - 5 Problems
Master of sort_values() by multiple columns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Sorting a DataFrame by two columns
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Anna', 'Bob', 'Charlie', 'Bob', 'Anna'], 'Age': [25, 30, 35, 22, 25], 'Score': [88, 92, 85, 95, 90] }) sorted_df = df.sort_values(by=['Name', 'Age']) print(sorted_df.reset_index(drop=True))
Attempts:
2 left
💡 Hint
Remember that sort_values sorts first by the first column, then by the second within groups.
✗ Incorrect
The DataFrame is sorted first by 'Name' alphabetically, then by 'Age' ascending within each 'Name'. Both 'Anna' rows have Age 25, so they appear first, followed by 'Bob' rows sorted by Age 22 then 30, then 'Charlie'.
❓ data_output
intermediate1:30remaining
Number of rows after sorting with duplicates
After sorting the DataFrame below by 'Category' and 'Value', how many rows does the sorted DataFrame contain?
Pandas
import pandas as pd df = pd.DataFrame({ 'Category': ['A', 'B', 'A', 'C', 'B', 'A'], 'Value': [10, 20, 10, 30, 20, 15] }) sorted_df = df.sort_values(by=['Category', 'Value']) print(len(sorted_df))
Attempts:
2 left
💡 Hint
Sorting does not remove rows, it only changes their order.
✗ Incorrect
Sorting the DataFrame does not change the number of rows. The original DataFrame has 6 rows, so the sorted DataFrame also has 6 rows.
🔧 Debug
advanced2:00remaining
Identify the error in sorting by multiple columns
What error will this code raise?
Pandas
import pandas as pd df = pd.DataFrame({ 'X': [1, 2, 3], 'Y': [4, 5, 6] }) sorted_df = df.sort_values(by='X', 'Y')
Attempts:
2 left
💡 Hint
Check the function call syntax and argument order.
✗ Incorrect
The code passes 'by' as a keyword argument and then another positional argument 'Y'. Python does not allow positional arguments after keyword arguments, causing a SyntaxError.
🚀 Application
advanced2:00remaining
Sorting with different ascending orders
You want to sort a DataFrame by 'City' ascending and 'Temperature' descending. Which code achieves this?
Pandas
import pandas as pd df = pd.DataFrame({ 'City': ['Paris', 'Paris', 'London', 'London'], 'Temperature': [20, 25, 15, 10] })
Attempts:
2 left
💡 Hint
The ascending parameter can be a list matching the columns.
✗ Incorrect
To sort 'City' ascending and 'Temperature' descending, pass ascending=[True, False]. Other options either reverse the order or apply the same order to both columns.
🧠 Conceptual
expert1:30remaining
Effect of inplace parameter in sort_values()
What is the effect of using inplace=true in the sort_values() method when sorting by multiple columns?
Attempts:
2 left
💡 Hint
Recall what inplace=true means for pandas methods.
✗ Incorrect
Using inplace=true modifies the original DataFrame directly and returns null. This works regardless of how many columns are used for sorting.