Skip to main content
Albero Academy

loc & iloc

Select rows and columns together — by label (loc) or position (iloc).

Selecting & Filtering9 min

Two accessors select rows and columns at once. loc selects by label; iloc selects by integer position. They're the workhorses of precise selection.

loc vs iloc

AccessorSelects byExample
locRow/column labelsdf.loc[0, "name"]
ilocInteger positionsdf.iloc[0, 1]
the everyday patterns
df.loc[0, "name"] # a single cell by label
df.loc[df["package"] > 7, "name"] # filtered rows, one column — very common
df.loc[:, ["name", "package"]] # all rows, chosen columns
df.iloc[0] # first row by position
df.iloc[0:3, 0:2] # first 3 rows, first 2 columns

Setting values

Key takeaways

  1. 01loc selects by label; iloc selects by integer position.
  2. 02df.loc[rows, cols] is the precise workhorse — combine a row mask with column names.
  3. 03iloc uses 0-based positions and half-open ranges like NumPy.
  4. 04Set values through .loc to avoid the chained-assignment trap.

Finished this chapter?

Mark it complete to track your progress through Pandas.