0
0
Pandasdata~10 mins

Swapping index levels in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aswap_levels
Bswap_index
Cswitchlevel
Dswaplevel
Attempts:
3 left
💡 Hint
Common Mistakes
Using swap_index which does not exist.
Using swap_levels which is incorrect method name.
2fill in blank
medium

Complete 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'
Aswitchlevel
Bswaplevel
Cswap_index
Dswap_levels
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like switchlevel.
Passing level names to methods that don't accept them.
3fill in blank
hard

Fix 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'
A'0'
B1
C0
D'first'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string '0' instead of integer 0.
Using wrong level names that don't exist.
4fill in blank
hard

Fill 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'
Aswaplevel
Bswap_index
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using swap_index which is not a valid method.
Using wrong level numbers.
5fill in blank
hard

Fill 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'
Aswaplevel
B'level1'
C'level3'
Dswap_index
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method swap_index.
Passing level names without quotes.