Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Const

df.iloc

DataFrame.iloc selects rows and columns by zero-based integer position. It supports scalar, list-like, slice, boolean, callable, and row-column tuple indexers.

How it works

DataFrame.iloc provides zero-based, integer-position indexing. It accepts scalar positions, integer collections, integer slices, boolean arrays, callables, or row-and-column tuples; slices may extend beyond the axis bounds without raising an error.

Syntax

DataFrame.iloc[indexer]

Key points

  • Use iloc when the selection should depend on physical row or column positions rather than labels.
  • Positions are zero-based and run through the final valid position on each axis.
  • An integer collection can select multiple positions and can specify them in a custom order.
  • Integer slices select a positional range using normal Python slice semantics.
  • A boolean array can filter rows or columns when its length matches the targeted axis.
  • A callable receives the calling object and can produce a valid positional indexer, which supports method-chain selections.
  • A two-part tuple lets one iloc expression address row positions and column positions together.

Common mistakes

  • Scalar, list-like, and boolean indexers must stay within the DataFrame bounds; otherwise iloc raises IndexError.
  • Unlike other indexers, slices tolerate endpoints outside the available positional range.

Example: Select DataFrame values by position

import pandas as pd

df = pd.DataFrame({"name": ["Ada", "Linus", "Grace"], "score": [98, 91, 95]})

first_row = df.iloc[0]
selected = df.iloc[[0, 2], [0, 1]]
window = df.iloc[0:2, 1]

print(first_row["name"])
print(selected)
print(window.tolist())

This selects one row, selected rows and columns, and a positional slice.

When to use it

Position-based selection

Extracting rows or columns by their physical order when labels are unknown or irrelevant.

Two-axis extraction

Selecting a rectangular subset with independent row and column positions.

Method-chain selection

Filtering within a chained transformation using a callable indexer.

Interview questions

QWhat does DataFrame.iloc do?

iloc selects by integer position rather than by index labels. Position counting starts at zero, so iloc[0] addresses the first row regardless of that row's label.

Follow-up: How would you select rows 2 through 4 and columns 0 and 3? Use df.iloc[2:5, [0, 3]], because the slice stop is exclusive.

QWhich indexers can iloc accept?

It accepts an integer, an integer list or array, an integer slice, a boolean array, a callable returning one of those indexers, or a row-and-column tuple. A tuple supplies separate positional selectors for the two axes.

Follow-up: When is a callable useful? It enables selection in a method chain when the DataFrame reference is not otherwise named.

QHow does iloc handle out-of-bounds indexing?

A scalar position, list-like position, or boolean indexer outside the valid range raises IndexError. Slice indexers are the exception and follow Python/NumPy slice behavior, allowing out-of-bounds endpoints.

Follow-up: What does df.iloc[:, 1] mean? It selects every row from the second column, since the row slice is unrestricted and column positions are zero-based.

  • pandas.DataFrame.loc
  • pandas.Series.iloc
  • pandas.DataFrame

Source: pandas.DataFrame.iloc. Checked 2026-10-15.