0
0
Pandasdata~10 mins

loc for label-based selection 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 row with label 'b' using loc.

Pandas
selected_row = df.loc[[1]]
Drag options to blanks, or click blank then click option'
A'a'
B'b'
C'c'
D'd'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer index instead of label.
Using iloc instead of loc.
2fill in blank
medium

Complete the code to select the 'Age' column for the row labeled 'c'.

Pandas
age_value = df.loc[[1], 'Age']
Drag options to blanks, or click blank then click option'
A'c'
B'b'
C'a'
D'd'
Attempts:
3 left
💡 Hint
Common Mistakes
Using column name as first argument.
Using integer index instead of label.
3fill in blank
hard

Fix the error in the code to select rows labeled 'a' to 'c' inclusive.

Pandas
subset = df.loc[[1]]
Drag options to blanks, or click blank then click option'
A'a','c'
B['a':'c']
C'a':'c'
D['a', 'b', 'c']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string slice inside a list.
Using incorrect syntax for slicing.
4fill in blank
hard

Fill both blanks to select the 'Name' and 'Age' columns for rows labeled from 'b' to 'd' inclusive.

Pandas
subset = df.loc[[1], [2]]
Drag options to blanks, or click blank then click option'
A'b':'d'
B['Name', 'Age']
C'Name'
D'Age'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single column names instead of a list for multiple columns.
Using incorrect slicing syntax.
5fill 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: {BLANK_2}} for word in words if {{BLANK_2}}
Drag options to blanks, or click blank then click option'
A:
Blen(word)
Clen(word) > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Putting a colon inside quotes in the first blank.
Using the wrong condition in the if statement.
Confusing key and value positions.