0
0
Pandasdata~5 mins

loc vs iloc mental model in Pandas - Quick Revision & Key Differences

Choose your learning style9 modes available
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?
Aloc
Biloc
Cix
Dat
What does df.iloc[0] return?
AFirst row by label
BLast row by position
CFirst row by position
DLast row by label
If row labels are [1, 3, 5], what does df.loc[3] return?
ARow at position 3
BRow at position 1
CError
DRow with label 3
Which slicing includes the end label in pandas?
Ailoc slicing
Bloc slicing
CBoth loc and iloc
DNeither
What happens if you use df.iloc[5] on a DataFrame with 5 rows?
ARaises an IndexError
BReturns the last row
CReturns the 6th row
DReturns the first row
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.