0
0
Pandasdata~10 mins

Sorting MultiIndex 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 sort the MultiIndex DataFrame by its index.

Pandas
sorted_df = df.[1]()
Drag options to blanks, or click blank then click option'
Asort_values
Bsort_index
Corder_by
Darrange
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort_values() instead of sort_index()
Trying to use order_by() which is not a pandas method
2fill in blank
medium

Complete the code to sort the MultiIndex DataFrame by the second level of the index.

Pandas
sorted_df = df.sort_index(level=[1])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D'first'
Attempts:
3 left
💡 Hint
Common Mistakes
Using level=0 which sorts by the first level
Using a string 'first' which is not a valid level
3fill in blank
hard

Fix the error in the code to sort the MultiIndex DataFrame in descending order.

Pandas
sorted_df = df.sort_index(ascending=[1])
Drag options to blanks, or click blank then click option'
ATrue
BNone
C'descending'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string 'descending' instead of a boolean
Using True which sorts ascending
4fill in blank
hard

Fill both blanks to sort the MultiIndex DataFrame by the first level in descending order.

Pandas
sorted_df = df.sort_index(level=[1], ascending=[2])
Drag options to blanks, or click blank then click option'
A0
B1
CFalse
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using level=1 instead of 0
Using ascending=True which sorts ascending
5fill in blank
hard

Fill all three blanks to sort the MultiIndex DataFrame by both levels, first ascending then descending.

Pandas
sorted_df = df.sort_index(level=[[1], [2]], ascending=[[3], False])
Drag options to blanks, or click blank then click option'
A0
B1
CTrue
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong level numbers
Using ascending=False for both levels