iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use .loc when you mean an index or column label; use .iloc when you mean a zero-based position. An integer passed to .loc is still a label, not a position.
What .loc and .iloc select
Consider a DataFrame whose row index contains names rather than numbers:
import pandas as pd
df = pd.DataFrame(
{"name": ["Ada", "Ben", "Cy"], "score": [91, 84, 88]},
index=["a", "b", "c"]
)
Here, df.loc["b"] returns the row labeled b, while df.iloc[1] returns the second row. They happen to select the same row in this example, but they use different rules: one looks up a label and the other counts from position zero.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsThe current stable pandas indexing guide describes .loc as primarily label-based, while noting that it can also accept a boolean array. The advanced indexing guide and introductory tutorial show related indexing patterns. These documentation pages are identified as pandas 3.0.x; if you need to confirm behavior for an older release, consult that release’s documentation.
#1 Best Overall
Why integer indexes cause confusion
With pandas’ usual default index, the rows are labeled 0, 1, and so on. Consequently, df.loc[0] often appears to mean “first row” because the first row’s label is 0. It means “the row whose label is 0.” By contrast, df.iloc[0] means “the row at position zero.”
Those meanings diverge when labels are changed or reordered. For example, if a DataFrame has index labels [10, 20, 30], df.loc[10] selects the row labeled 10, while df.iloc[0] selects the first row. Choose based on what the number represents, not on its type.
Rank #2
How row and column selection works
Both accessors can select rows and columns in one operation. Separate the row selector from the column selector with a comma:
Free tools Windows power users keep installed
One-click scans. No signup required.
df.loc[row_labels, column_labels]uses labels on both axes.df.iloc[row_positions, column_positions]uses integer positions on both axes.
For the example DataFrame, df.loc["b", "score"] selects the score at row label b, while df.iloc[1, 1] selects the value at the second row and second column. For multiple labels or positions, pass a list, such as df.loc[["a", "c"], ["name", "score"]] or df.iloc[[0, 2], [0, 1]].
Slice endpoints are different
A slice’s stop value follows a different rule for each accessor. A .loc slice includes the stop label when it is present; an .iloc slice excludes the stop position, like an ordinary Python slice.
df.loc["a":"b"] # rows labeled a and b
df.iloc[0:2] # rows at positions 0 and 1
In this example both expressions return the first two rows. Do not assume that the same-looking start and stop values will select the same rows if the index labels differ from the row positions.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Missing labels, invalid positions, and boolean selectors
- Missing label: Asking
.locfor a label that is not present raisesKeyError. - Out-of-range position: Asking
.ilocfor an integer position outside the axis raisesIndexError. Slice indexers may extend beyond axis bounds, following Python/NumPy slice behavior. - Boolean selection: Both accessors accept boolean arrays. A boolean Series can be used with
.locso its index can align with the DataFrame..ilocexpects a boolean array rather than an index-aligned Series; use the Series’ values when appropriate, for examplemask.to_numpy().
The pandas indexing guide states that missing values in boolean arrays are treated as false. Check that a boolean mask is in the intended row order when using it positionally.
Quick Recap
Best Value
A quick way to choose
- You know the row or column name: use
.loc. - You know the row or column’s zero-based place: use
.iloc. - You are slicing labels and want to include the ending label: use
.loc. - You are slicing positions and want the usual exclusive stop: use
.iloc.
Memory aid: labels → .loc; positions → .iloc.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

