All lessons

Module 2: Selecting and filtering · Lesson 2.2 · about 20 min

Rows and cells with loc and iloc

loc picks rows and columns by label, iloc by position. Read one cell, a block of rows and columns, rows matching a condition, and change values safely with loc.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 2 · about 20 minutes · uses Lesson 2.1. You’ll be able to pick any rows and columns by label or position, and change values the right way.

loc picks rows and columns by their labels (and by conditions); iloc picks them by position, like counting cells.

In everyday terms: loc is finding a house by its street address; iloc is “the third house from the corner”. Addresses stay right when the houses are re-ordered; counting does not.

1. loc: by label

df.loc[rows, columns]: say which rows and which columns, by their labels.

Pythonstores.csv

index_col='store_id' made the ids the row labels, so loc can find a store by its id. Label slices like 'S02':'S04' include the end.

2. iloc: by position

df.iloc[rows, columns]: positions counted from 0, like a Python list. The end of a slice is not included.

Pythonorders.csv

Use iloc for “the first n” or “the last row”; use loc for everything with names, which keeps working when the column order changes.

3. Conditions with loc

loc also takes a True/False Series for the rows, which makes it the safe way to change values:

Pythonorders.csv

Don’t write orders[big]['discount'] = 0.15: the first brackets make a temporary table and the change is lost (pandas 3 warns and ignores it). One loc with both parts always works.

df.loc[rows, cols]
By label; slices include both ends.
df.iloc[rows, cols]
By position from 0; the end of a slice is excluded.
df.loc[mask, 'col']
Rows where the condition is True, one column.
Physical meaning
Labels belong to the data; positions belong to the current order. After sorting or filtering, label 5 may be in position 0. Use loc for meaning, iloc for "the first few".

“loc[0:2] and iloc[0:2] give the same rows.” loc includes label 2 (3 rows, if labels are 0, 1, 2); iloc stops before position 2 (2 rows). After sorting, they may not even be the same rows.

Watch it happen, one step at a time:

A sorted table.
labelstoreqty
7WEB9
2S018
5S036
iloc[0] → the first row in position: label 7, WEB.
loc[2] → the row whose label is 2: S01, wherever it sits.
loc with a condition. df.loc[df['qty'] > 7, 'store'] → WEB, S01.

4. Your turn

Your turnstores.csv
Your turnproducts.csv
How it works

Principle: every row has a label (its index value) and a position (0, 1, 2, …). After sorting or filtering they differ: the row labelled 57 might now be in position 0. loc uses labels; iloc uses positions; neither guesses.

Pythonorders.csv

5. Practice

  1. Write the code for rows 10 to 19 (by position) and columns date and qty.
  2. After sorting, why might df.loc[0] not be the first row?
  3. Set segment to 'VIP' where customer_id is 'C0001'.
Answers
  1. df.iloc[10:20][['date', 'qty']], or df.loc[df.index[10:20], ['date', 'qty']].
  2. loc[0] finds the row labelled 0, wherever sorting moved it.
  3. customers.loc[customers['customer_id'] == 'C0001', 'segment'] = 'VIP'.

New words: loc, iloc, label, position, index_col, chained assignment.

  • loc = labels (and boolean masks); iloc = positions.
  • loc slices include the end label; iloc slices exclude the end position.
  • df.loc[mask, ‘col’] filters and selects in one step.
  • Use loc to change values: df.loc[mask, ‘col’] = value.

Check yourself

1df.iloc[0] gives what?
2How do you set discount to 0.1 for every row where store_id is 'S03'?
3Does df.loc[2:4] include row label 4?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.