Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the two levels of the MultiIndex in the DataFrame df.
Pandas
df_swapped = df.[1](0, 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
swap_index which does not exist.Using
swap_levels which is incorrect method name.✗ Incorrect
The
swaplevel method swaps two levels of a MultiIndex in a DataFrame or Series.2fill in blank
mediumComplete the code to swap the index levels named 'first' and 'second' in the DataFrame df.
Pandas
df_swapped = df.[1]('first', 'second')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like
switchlevel.Passing level names to methods that don't accept them.
✗ Incorrect
You can specify level names in
swaplevel to swap those index levels.3fill in blank
hardFix the error in the code to swap the first and second index levels of df.
Pandas
df_swapped = df.swaplevel([1], 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string '0' instead of integer 0.
Using wrong level names that don't exist.
✗ Incorrect
Index levels are specified by integer position or exact name. Using integer 0 is correct for the first level.
4fill in blank
hardFill both blanks to swap the index levels 0 and 2 in df.
Pandas
df_swapped = df.[1]([2], 2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
swap_index which is not a valid method.Using wrong level numbers.
✗ Incorrect
Use
swaplevel method and specify level 0 and 2 to swap those index levels.5fill in blank
hardFill all three blanks to swap index levels 'level1' and 'level3' in df and assign the result to df_swapped.
Pandas
df_swapped = df.[1]([2], [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method
swap_index.Passing level names without quotes.
✗ Incorrect
Use
swaplevel with the exact level names as strings to swap those index levels.