Challenge - 5 Problems
MultiIndex Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of MultiIndex creation from tuples
What is the output of this code snippet that creates a MultiIndex DataFrame from tuples?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1)]) df = pd.DataFrame({'Value': [10, 20, 30]}, index=index) print(df)
Attempts:
2 left
💡 Hint
Look at how pandas displays MultiIndex DataFrames with hierarchical row labels.
✗ Incorrect
The MultiIndex shows two levels: first level with 'A' and 'B', second level with 1 and 2. The DataFrame prints with indentation for the second level.
❓ data_output
intermediate1:30remaining
Number of rows in MultiIndex DataFrame
Given this MultiIndex DataFrame, how many rows does it contain?
Pandas
import pandas as pd arrays = [['X', 'X', 'Y', 'Y'], [10, 20, 10, 20]] index = pd.MultiIndex.from_arrays(arrays, names=('Letter', 'Number')) df = pd.DataFrame({'Score': [5, 15, 10, 20]}, index=index) print(len(df))
Attempts:
2 left
💡 Hint
Count the number of unique index pairs created.
✗ Incorrect
The MultiIndex has 4 pairs: ('X',10), ('X',20), ('Y',10), ('Y',20). So the DataFrame has 4 rows.
🔧 Debug
advanced2:00remaining
Identify the error in MultiIndex creation
What error does this code raise when trying to create a MultiIndex DataFrame?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('B',)]) df = pd.DataFrame({'Value': [10, 20]}, index=index)
Attempts:
2 left
💡 Hint
Check if all tuples have the same length for MultiIndex.
✗ Incorrect
MultiIndex.from_tuples requires all tuples to have the same length. Here, ('A', 1) has length 2 but ('B',) has length 1, causing ValueError.
🚀 Application
advanced2:30remaining
Create MultiIndex DataFrame from product of lists
Which code snippet correctly creates a MultiIndex DataFrame with all combinations of ['Red', 'Blue'] and [1, 2] as index levels?
Attempts:
2 left
💡 Hint
Use from_product to get all combinations of the two lists.
✗ Incorrect
from_product creates the cartesian product of the lists, producing 4 index pairs. The DataFrame has 4 rows matching these pairs.
🧠 Conceptual
expert3:00remaining
Understanding MultiIndex level names and access
Given a MultiIndex DataFrame with levels named 'City' and 'Year', which code correctly selects all rows where 'City' is 'Paris'?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('Paris', 2020), ('Paris', 2021), ('London', 2020)], names=['City', 'Year']) df = pd.DataFrame({'Population': [2_100_000, 2_150_000, 8_900_000]}, index=index)
Attempts:
2 left
💡 Hint
Use the cross-section method to select by level name.
✗ Incorrect
df.xs('Paris', level='City') selects all rows where the 'City' level equals 'Paris'. df.loc['Paris'] works only if 'City' is the outermost index level, which it is here, but xs is more explicit and reliable.