Challenge - 5 Problems
MultiIndex Reset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of resetting MultiIndex to columns
What is the output DataFrame after resetting the MultiIndex to columns in the following code?
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) result = df.reset_index()
Attempts:
2 left
💡 Hint
Resetting MultiIndex moves index levels to columns named after the index levels or default names.
✗ Incorrect
reset_index() converts each level of the MultiIndex into a separate column. The columns are named after the index levels or default names like 'level_0', 'level_1'. The original index is replaced by a default integer index.
❓ data_output
intermediate1:30remaining
Number of columns after resetting MultiIndex
Given a DataFrame with a MultiIndex of 3 levels and 2 data columns, how many columns will the DataFrame have after resetting the MultiIndex?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('X', 'Y', 'Z'), ('X', 'Y', 'W')]) df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=index) result = df.reset_index()
Attempts:
2 left
💡 Hint
Each MultiIndex level becomes a column plus the original data columns remain.
✗ Incorrect
The MultiIndex has 3 levels, so resetting it adds 3 columns. The original 2 data columns remain. Total columns = 3 + 2 = 5.
🔧 Debug
advanced2:00remaining
Error when resetting MultiIndex with drop=True
What error will this code raise and why?
import pandas as pd
index = pd.MultiIndex.from_tuples([('a', 1), ('b', 2)])
df = pd.DataFrame({'val': [100, 200]}, index=index)
result = df.reset_index(drop=True)
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('a', 1), ('b', 2)]) df = pd.DataFrame({'val': [100, 200]}, index=index) result = df.reset_index(drop=True)
Attempts:
2 left
💡 Hint
drop=True removes the index instead of converting it to columns.
✗ Incorrect
reset_index(drop=True) removes the index and replaces it with a default integer index. This works fine even with MultiIndex.
🚀 Application
advanced2:00remaining
Using reset_index to prepare data for CSV export
You have a DataFrame with a MultiIndex and want to save it to CSV with all index levels as columns. Which code snippet correctly prepares the DataFrame before saving?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('cat', 'small'), ('dog', 'large')]) df = pd.DataFrame({'weight': [5, 20]}, index=index)
Attempts:
2 left
💡 Hint
To save index levels as columns, reset the index first without dropping it.
✗ Incorrect
reset_index() moves MultiIndex levels to columns. Then saving with index=False avoids saving the old index again.
🧠 Conceptual
expert2:30remaining
Effect of reset_index on MultiIndex with named levels
Consider a DataFrame with a MultiIndex whose levels are named 'City' and 'Year'. After calling reset_index(), what will be the names of the new columns created from the index?
Attempts:
2 left
💡 Hint
Named index levels become columns with the same names after reset_index.
✗ Incorrect
When MultiIndex levels have names, reset_index() uses those names as column names instead of default 'level_0', 'level_1'.