0
0
Pandasdata~10 mins

Selecting data with 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 select the data for the first level index 'A' from the MultiIndex DataFrame.

Pandas
selected = df.loc[[1]]
Drag options to blanks, or click blank then click option'
A'D'
B'B'
C'C'
D'A'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer positions instead of labels.
Using .iloc instead of .loc for label-based selection.
2fill in blank
medium

Complete the code to select data for the MultiIndex where the first level is 'B' and the second level is 'two'.

Pandas
selected = df.loc[([1], 'two')]
Drag options to blanks, or click blank then click option'
A'D'
B'A'
C'B'
D'C'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a tuple for multi-level selection.
Swapping the order of levels in the tuple.
3fill in blank
hard

Fix the error in the code to select all rows where the second level index is 'one'.

Pandas
selected = df.xs([1], level=1)
Drag options to blanks, or click blank then click option'
A'one'
B1
C'two'
D'zero'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong level number.
Using an integer instead of the label string.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

Pandas
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word in the condition instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if the count is greater than 1.

Pandas
result = [1]: [2] for [3], [2] in counts.items() if [2] > 1}}
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
Cword
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable names in the loop.
Not converting words to uppercase.
Using the count variable incorrectly.