0
0
Pandasdata~10 mins

loc vs iloc mental model in Pandas - Interactive 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 2 using loc.

Pandas
row = df.loc[[1]]
Drag options to blanks, or click blank then click option'
A'2'
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using iloc syntax with loc
Using string '2' instead of integer 2
2fill in blank
medium

Complete the code to select the third row by position using iloc.

Pandas
row = df.iloc[[1]]
Drag options to blanks, or click blank then click option'
A2
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using label instead of position
Counting rows starting at 1
3fill in blank
hard

Fix the error in selecting rows 1 to 3 (inclusive) using loc.

Pandas
subset = df.loc[[1]]
Drag options to blanks, or click blank then click option'
A[1,3]
B[1:3]
C1:4
D1:3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1:4 which includes label 4
Using list syntax instead of slice
4fill in blank
hard

Fill both blanks to select rows 0 to 2 by position and columns 'A' and 'B' by label.

Pandas
subset = df.iloc[[1]].loc[:, [2]]
Drag options to blanks, or click blank then click option'
A0:3
B['A', 'B']
C['B', 'A']
D0:2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0:2 which excludes row 2
Swapping column labels order
5fill in blank
hard

Fill all three blanks to create a dictionary of word lengths for words longer than 3 letters.

Pandas
lengths = {word: [1] for word in words if [2] [3] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Not using len(word) in condition