.iloc[] (Pandas)
.iloc[] selects rows and columns by their integer position, ignoring the labels entirely. It is position-based indexing and, like normal Python slicing, excludes the stop index.
Pandas
Where .loc speaks in labels, .iloc speaks in 0-based positions: df.iloc[0] is always the first row no matter what the index says, and df.iloc[0:3] returns the first three rows because the stop is exclusive — the opposite of .loc.
Both axes accept the same things: a single integer, a list of integers, a slice, or a boolean array. df.iloc[-1] grabs the last row, mirroring negative indexing on Python lists.
Use .iloc when position is what matters — the first row, the last column, every other row — and use .loc when you want to refer to data by name. Mixing the two (df.loc[0] on a non-integer index) is a frequent source of bugs.
Syntax
df.iloc[row_positions, column_positions]Examples
Python
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]}, index=["x", "y", "z"])
print(df.iloc[0, 0])Output:
1