Challenge - 5 Problems
MultiIndex Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sorting a MultiIndex DataFrame by level
What is the output of the following code snippet?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('b', 2), ('a', 1), ('b', 1), ('a', 2)]) df = pd.DataFrame({'value': [4, 3, 2, 1]}, index=index) sorted_df = df.sort_index(level=0) print(sorted_df)
Attempts:
2 left
💡 Hint
Sorting by level=0 sorts by the first index level alphabetically.
✗ Incorrect
The DataFrame is sorted by the first level of the MultiIndex ('a' before 'b'). Within each group, the order of the second level remains as in the original data.
❓ data_output
intermediate1:30remaining
Number of rows after sorting MultiIndex with ascending=False
Given the following DataFrame, how many rows does the sorted DataFrame have after sorting by both levels descending?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('x', 3), ('y', 1), ('x', 1), ('y', 2)]) df = pd.DataFrame({'score': [10, 20, 30, 40]}, index=index) sorted_df = df.sort_index(level=[0,1], ascending=False) print(len(sorted_df))
Attempts:
2 left
💡 Hint
Sorting does not remove rows, it only reorders them.
✗ Incorrect
Sorting by both levels descending keeps all rows but changes their order. The number of rows remains 4.
🔧 Debug
advanced1:30remaining
Identify the error in sorting MultiIndex with invalid level
What error does the following code raise?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('a', 1), ('b', 2)]) df = pd.DataFrame({'val': [5, 6]}, index=index) sorted_df = df.sort_index(level=2) print(sorted_df)
Attempts:
2 left
💡 Hint
Check the number of levels in the MultiIndex before sorting by level.
✗ Incorrect
The MultiIndex has only 2 levels (0 and 1). Trying to sort by level=2 causes an IndexError about too many levels.
🚀 Application
advanced2:00remaining
Sorting MultiIndex DataFrame by multiple levels with mixed ascending order
You have a MultiIndex DataFrame with two levels: 'city' and 'year'. You want to sort it so that cities are in ascending order but years are in descending order. Which code snippet achieves this?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('NY', 2020), ('LA', 2019), ('NY', 2019), ('LA', 2020)]) df = pd.DataFrame({'population': [8, 4, 7, 5]}, index=index)
Attempts:
2 left
💡 Hint
The ascending parameter accepts a list matching the levels order.
✗ Incorrect
To sort 'city' ascending and 'year' descending, pass ascending=[True, False] with level order ['city', 'year'].
🧠 Conceptual
expert1:30remaining
Effect of sort_index on MultiIndex with duplicates
Consider a MultiIndex DataFrame with duplicate index entries. What is the effect of calling sort_index() on this DataFrame?
Attempts:
2 left
💡 Hint
Think about whether sorting changes data or just order.
✗ Incorrect
sort_index() only reorders rows based on index levels. It does not remove duplicates or reset the index.