Selecting a row of pandas series/dataframe by integer index
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In pandas, integer position and index label are two different concepts. If you want the row at position 2, the usual tool is .iloc. If you want the row whose index label equals 2, the tool is .loc. Mixing those two ideas is one of the most common pandas indexing mistakes.
Use .iloc for Integer Position
.iloc always interprets integers as zero-based positions.
A single position returns a Series. A list of positions or a slice returns a DataFrame.
.loc Uses Labels, Even When Labels Are Integers
This is where people get caught. .loc uses index labels, not positions.
These happen to point at the same row only because the label 20 is currently at position 1. After sorting or resetting the index, that coincidence can disappear immediately.
The Same Rule Applies to Series
Series objects follow the same distinction.
Again, the first uses position and the second uses label. Writing the access method explicitly makes the code clearer and safer.
Use .iat for a Single Scalar
If you need exactly one cell by row and column position, .iat is the most direct choice.
This is often a better fit than selecting a row first and then indexing again.
Write Defensive Position-Based Code
If the requested row position may be out of range, wrap the access so the failure mode is explicit.
This is useful in notebooks, ETL code, or exploratory scripts where an invalid position should not crash the whole workflow.
Select Rows and Columns Together
.iloc can also select by row and column position in one step.
This selects rows at positions 1 and 2, and columns at positions 0 and 1.
Be Careful After Sorting or Resetting the Index
Position-based code depends on row order, so it can silently change meaning after operations such as sort_values, filtering, or reset_index.
This now returns the highest-scoring row, not necessarily the row that originally appeared first. When row identity matters, a stable key column is often safer than positional selection.
Common Pitfalls
A common mistake is using .loc when you actually mean position. This bug is especially easy to miss when the index itself contains integers.
Another is forgetting that .iloc[1:3] follows normal Python slicing rules, so the stop bound is excluded.
Developers also sometimes expect .iloc[2] to return a one-row DataFrame. It returns a Series. If you need a DataFrame, use .iloc[[2]] instead.
Summary
- Use
.ilocwhen integers represent row positions. - Use
.locwhen values represent index labels. - Use
.iatfor a single scalar by row and column position. - Remember that
.ilocslices exclude the stop bound. - Use
.iloc[[n]]when you need a one-rowDataFramerather than aSeries.

