0
0
Pandasdata~10 mins

iloc for position-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 first row of the DataFrame using iloc.

Pandas
first_row = df.iloc[[1]]
Drag options to blanks, or click blank then click option'
A0
B-1
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 to select the first row.
Using negative indices incorrectly.
2fill in blank
medium

Complete the code to select the first three rows of the DataFrame using iloc.

Pandas
subset = df.iloc[[1]]
Drag options to blanks, or click blank then click option'
A0,3
B1:4
C0:3
D[0,1,2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1:4 which skips the first row.
Using a tuple or list instead of a slice.
3fill in blank
hard

Fix the error in the code to select the last two rows using iloc.

Pandas
last_two = df.iloc[[1]]
Drag options to blanks, or click blank then click option'
A-2:
B[-2:]
Cdf[-2:]
Dlen(df)-2:len(df)
Attempts:
3 left
💡 Hint
Common Mistakes
Using brackets around the slice which causes a TypeError.
Using df[-2:] which is label-based slicing, not iloc.
4fill in blank
hard

Fill both blanks to select rows 1 to 3 and columns 0 to 2 using iloc.

Pandas
subset = df.iloc[[1], [2]]
Drag options to blanks, or click blank then click option'
A1:4
B0:3
C[1,2,3]
D[0,1,2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of slices for iloc.
Off-by-one errors in slice ranges.
5fill in blank
hard

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

Pandas
lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the length as the key instead of the value.