Recall & Review
beginner
What does
loc do in pandas?loc selects data by label. It uses the row and column names you see in the DataFrame.
Click to reveal answer
beginner
What does
iloc do in pandas?iloc selects data by position. It uses the row and column numbers starting from 0.
Click to reveal answer
beginner
If a DataFrame has row labels
[10, 20, 30], what will df.loc[20] return?It returns the row with label 20. Labels are exact matches.
Click to reveal answer
beginner
If a DataFrame has 5 rows, what will
df.iloc[2] return?It returns the third row (index 2) because iloc counts rows by position starting at 0.
Click to reveal answer
intermediate
How does slicing differ between
loc and iloc?loc slices include the end label (inclusive). iloc slices exclude the end position (like normal Python slices).
Click to reveal answer
Which method uses row and column labels to select data?
✗ Incorrect
loc uses labels. iloc uses positions.
What does
df.iloc[0] return?✗ Incorrect
iloc selects by position, so 0 means the first row.
If row labels are
[1, 3, 5], what does df.loc[3] return?✗ Incorrect
loc uses labels, so it returns the row labeled 3.
Which slicing includes the end label in pandas?
✗ Incorrect
loc slices include the end label; iloc slices exclude the end position.
What happens if you use
df.iloc[5] on a DataFrame with 5 rows?✗ Incorrect
iloc uses zero-based positions. Index 5 is out of range for 5 rows, so it raises an error.
Explain the difference between
loc and iloc in pandas using a real-life example.Think about how you find items by name versus by order.
You got /3 concepts.
Describe how slicing works differently with
loc and iloc.Compare to how Python normally slices lists.
You got /3 concepts.