Skip to content
LearnPY
pandasfunction reference

DataFrame.loc

Select rows and columns by label.

Signature

DataFrame.loc[row_indexer, column_indexer]

What it does

.loc uses labels, including boolean arrays, slices with inclusive endpoints, and lists of column names. It is the clearest choice when your index carries meaning.

Parameters

row_indexerlabel, slice, list, or boolean array
Rows to select by label or condition.
column_indexerlabel, slice, list, or boolean array
Columns to select by label.

Returns

Series or DataFrame

Example

pythonDataFrame.loc example
import pandas as pd

df = pd.DataFrame({
    "team": ["red", "blue", "red"],
    "score": [10, 12, 8],
}, index=["a", "b", "c"])

print(df.loc["b", "score"])
print(df.loc[df["score"] > 9, ["team", "score"]])

Watch out for

  • Label slices include the stop label, unlike Python list slices.
  • .loc is label-based; use .iloc for integer positions.