How are iloc and loc different?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
iloc and loc both select data in pandas, but they answer different questions. iloc means "give me rows and columns by position." loc means "give me rows and columns by label."
That sounds simple until your index itself contains integers. Then many beginners expect loc[0] and iloc[0] to mean the same thing, but they often do not.
iloc Is Position-Based
iloc uses zero-based integer positions, just like normal Python sequence indexing:
This selects:
- the first row by position
- the value in first row, second column by position
iloc does not care what the row labels are.
loc Is Label-Based
loc uses the DataFrame's index labels and column labels:
This selects data by the actual row label "a" and the actual column label "score".
So:
- '
iloc[0]means first row' - '
loc["a"]means row whose label is"a"'
These are different ideas.
Slices Behave Differently
This is one of the most important differences.
With iloc, the stop position is excluded:
That returns positions 0 and 1.
With loc, label slices are inclusive at both ends:
That returns both "a" and "b".
This is a major source of off-by-one confusion when switching between the two.
Integer Indexes Make the Difference More Visible
Suppose the labels are integers:
Even though both use integers in the syntax, they are still doing different jobs.
That is why it is safer to think:
- '
ilocmeans position' - '
locmeans label'
and not "they both take numbers."
Boolean Filtering Works Naturally With loc
loc is often more readable with labeled conditions:
This is a very common pandas pattern: filter rows by a condition and select columns by name.
You can do boolean selection with iloc too, but loc usually reads more naturally when labels matter.
When to Prefer Each One
Use iloc when:
- you want the first or nth row regardless of label
- you are working from positions produced by another algorithm
- the structure is positional by nature
Use loc when:
- the index labels have meaning
- column names matter for readability
- you are filtering and selecting labeled subsets
In real pandas code, loc is often the more expressive choice because most data analysis is label-oriented.
Common Pitfalls
The biggest pitfall is assuming loc[0] means "first row." It means "row whose label is 0."
Another common mistake is forgetting that loc slices include the end label, while iloc slices exclude the end position.
People also write positional code with loc just because the labels happen to be numbers today. That breaks later when the index changes.
Finally, do not use deprecated mental models from the old .ix era. Modern pandas is clearer when you choose explicitly between label-based and position-based selection.
Summary
- '
ilocselects by integer position.' - '
locselects by row and column label.' - '
ilocslicing excludes the stop position;loclabel slicing includes the end label.' - Integer-looking labels do not turn
locinto position-based indexing. - Prefer
locfor readable label-based analysis andilocfor true positional access.

