Position-based selection
Extracting rows or columns by their physical order when labels are unknown or irrelevant.
Pandas · Const
DataFrame.iloc selects rows and columns by zero-based integer position. It supports scalar, list-like, slice, boolean, callable, and row-column tuple indexers.
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.
DataFrame.iloc[indexer]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.
Extracting rows or columns by their physical order when labels are unknown or irrelevant.
Selecting a rectangular subset with independent row and column positions.
Filtering within a chained transformation using a callable indexer.
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.
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.
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.locpandas.Series.ilocpandas.DataFrame