iloc
loc
pandas
data manipulation
Python programming

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:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"name": ["Ada", "Linus", "Grace"], "score": [95, 88, 99]},
5    index=["a", "b", "c"],
6)
7
8print(df.iloc[0])
9print(df.iloc[0, 1])

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:

python
print(df.loc["a"])
print(df.loc["a", "score"])

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:

python
print(df.iloc[0:2])

That returns positions 0 and 1.

With loc, label slices are inclusive at both ends:

python
print(df.loc["a":"b"])

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:

python
1df = pd.DataFrame(
2    {"value": [10, 20, 30]},
3    index=[100, 200, 300],
4)
5
6print(df.iloc[0])    # first row by position
7print(df.loc[100])   # row whose label is 100

Even though both use integers in the syntax, they are still doing different jobs.

That is why it is safer to think:

  • 'iloc means position'
  • 'loc means label'

and not "they both take numbers."

Boolean Filtering Works Naturally With loc

loc is often more readable with labeled conditions:

python
1df = pd.DataFrame(
2    {"name": ["Ada", "Linus", "Grace"], "score": [95, 88, 99]}
3)
4
5high_scores = df.loc[df["score"] >= 90, ["name", "score"]]
6print(high_scores)

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

  • 'iloc selects by integer position.'
  • 'loc selects by row and column label.'
  • 'iloc slicing excludes the stop position; loc label slicing includes the end label.'
  • Integer-looking labels do not turn loc into position-based indexing.
  • Prefer loc for readable label-based analysis and iloc for true positional access.

Course illustration
Course illustration

All Rights Reserved.