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
loc and iloc are pandas indexers, but they are not interchangeable. loc is label-based, while iloc is position-based, and that difference affects which inputs are valid, how slices behave, and which rows you actually get back.
Use loc when labels matter
loc selects rows and columns by their labels. If your index contains dates, IDs, or custom names, loc is usually the most readable choice.
The important rule is that label slices with loc are usually inclusive on both ends. That means "a":"c" returns rows a, b, and c.
Use iloc when position matters
iloc ignores labels and works with zero-based integer positions, which makes it behave more like normal Python indexing.
Here, 0:3 excludes position 3, just like normal Python slices do. That is one of the biggest differences from loc.
iloc is a better choice when your code means "first row," "last three columns," or "row number five regardless of label."
Integer labels are where confusion starts
Problems often appear when the DataFrame index itself contains integers. In that case, loc[2] and iloc[2] can point to different rows.
If the index labels happen to look like positions, it becomes easy to read code incorrectly. That is why many pandas bugs are not syntax problems. They are intent problems.
Filtering and assignment follow the same rule
The label-versus-position difference matters not only for reading data but also for modifying it.
In the first assignment, loc updates the row whose label is "b". In the second assignment, iloc updates the first row and first column by physical position.
When you are cleaning data or patching a subset of rows, choosing the wrong indexer can silently write to the wrong location.
Pick the indexer that matches the meaning of the data
A good mental rule is:
- use
locwhen the index is part of the meaning of the data - use
ilocwhen you care about row order or offset
For example, if your index is a timestamp or customer ID, label-based selection is usually the safer expression of intent. If you are sampling the first ten rows during exploratory work, iloc is more direct.
This becomes even more important in notebooks. DataFrames change shape over time, and code that relied on a row staying at position 5 can break semantically even if it still runs.
Common Pitfalls
The most common mistake is expecting loc slices to behave like Python slices. They usually include the stop label.
Another common issue is using iloc on data where the index labels are meaningful identifiers. That often works by accident until the row order changes.
People also get confused by integer-labeled indexes and assume loc[0] means "first row." It only means "row whose label is 0."
Finally, avoid mixing label names and integer positions in the same thought process. Pick the access model that matches your intent and stay consistent.
Summary
- '
locis label-based andilocis position-based.' - '
locslices are usually inclusive, whileilocslices follow normal Python rules.' - Integer-looking labels can make the difference easy to miss.
- The same rules apply to assignment, not just selection.
- Choose the indexer that matches whether your data is organized by meaning or by position.

