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.
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.
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.
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.
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:
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:
| label | store | qty |
|---|---|---|
| 7 | WEB | 9 |
| 2 | S01 | 8 |
| 5 | S03 | 6 |
df.loc[df['qty'] > 7, 'store'] → WEB, S01.4. Your turn
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.
5. Practice
- Write the code for rows 10 to 19 (by position) and columns
dateandqty. - After sorting, why might
df.loc[0]not be the first row? - Set
segmentto'VIP'wherecustomer_idis'C0001'.
Answers
df.iloc[10:20][['date', 'qty']], ordf.loc[df.index[10:20], ['date', 'qty']].loc[0]finds the row labelled 0, wherever sorting moved it.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
Sources
- pandas documentation, User Guide: Indexing and selecting data https://pandas.pydata.org/docs/user_guide/indexing.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
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.