SyntaxPilot
Courses/Data Analytics with Python/Lesson 14

Data Analytics with Python · Lesson 14 of 90 · 11 min

pandas 4: selecting with [], loc and iloc

column selectiondouble bracketslociloclabel vs positionslicing rowsatiatSettingWithCopyWarningchained assignmentview vs copy.copy()Copy-on-Write

These are the notes for Data Analytics with Python, episode 14. In episode 13 we loaded files into DataFrames. Now we select parts of a table: one column, several columns, rows by label with loc, rows by position with iloc, single cells with at and iat, and we change values safely. We also meet the pandas 3 rule that broke a lot of old code: Copy-on-Write, which ends chained assignment and removes the old SettingWithCopyWarning. Every term is defined the first time it appears and collected in the Glossary. Every program below was run for real (Python 3.13, pandas 3.0.5); outputs are unedited.

All examples use Ramesh's week of chai sales (from episode 12). The first line, sys.tracebacklimit = 0, only hides pandas' long internal traceback (the list of function calls Python prints before an error) so the error line is easy to read:

import sys; sys.tracebacklimit = 0
import pandas as pd
week = pd.DataFrame({
    "day": ["Mon", "Tue", "Wed", "Thu",
            "Fri", "Sat", "Sun"],
    "cups": [320, 280, 410, 300, 350, 390, 450],
    "price": [10, 10, 12, 10, 10, 12, 12],
    "weather": ["sunny", "rain", "cold", "sunny",
                "sunny", "cold", "rain"],
})

The problem: questions about parts of a table

The accountant keeps asking small questions: just Wednesday's cups; only the weather column; the last three days. Printing the whole table and reading it by eye works for 7 rows, not for 7 lakh, and eyes make mistakes. We need to point at exactly the part we want with code. Pointing at part of a table is called selecting or indexing.

Columns: [] and [[ ]]

print(week["cups"])
print(week["day", "cups"])
0    320
1    280
2    410
3    300
4    350
5    390
6    450
Name: cups, dtype: int64
KeyError: ('day', 'cups')

The above exception was the direct cause of the following exception:

KeyError: ('day', 'cups')
  • week["cups"] gives one column as a Series.
  • week["day", "cups"] fails. Two values separated by a comma make a tuple (episode 5), which is one key. pandas looked for a single column literally named ('day', 'cups').

The fix is to put a list of names inside the brackets, hence double brackets: the outer pair selects, the inner pair is a Python list.

print(week[["day", "cups"]])
print(type(week[["cups"]]))
   day  cups
0  Mon   320
1  Tue   280
2  Wed   410
3  Thu   300
4  Fri   350
5  Sat   390
6  Sun   450
<class 'pandas.DataFrame'>
Code Result
df["cups"] a Series
df[["cups"]] a DataFrame with one column
df[["day", "cups"]] a DataFrame with those columns, in that order

Rows: why df[2] fails, and df[2:4]

print(week[2])
KeyError: 2

The above exception was the direct cause of the following exception:

KeyError: 2

Square brackets with a single value always mean a column. pandas looked for a column named 2. Confusingly, a slice inside square brackets selects rows:

print(week[2:4])
   day  cups  price weather
2  Wed   410     12    cold
3  Thu   300     10   sunny

Two different rules for the same brackets is confusing, so pandas gives two clearer tools: loc and iloc.

loc vs iloc: label vs position

Think of a cinema. Your ticket says seat F12: a label printed on the seat. Someone else says "the 3rd seat from the aisle": a position, counted.

.loc[ ] .iloc[ ]
Short for location integer location
Selects by index labels and column names integer positions from 0
Slice end included excluded (like Python lists)
Negative numbers only if they are real labels count from the end (-1 = last)
Shape [rows, columns] [rows, columns]

Both take rows first, then a comma, then columns. A colon alone (:) means "all".

loc: by label

byday = week.set_index("day")
print(byday.loc["Wed"])
print(byday.loc["Wed", "cups"])
print(byday.loc["Tue":"Thu", ["cups", "weather"]])
cups        410
price        12
weather    cold
Name: Wed, dtype: object
410
     cups weather
day              
Tue   280    rain
Wed   410    cold
Thu   300   sunny
  • loc["Wed"]: one whole row, returned as a Series (its index is the column names).
  • loc["Wed", "cups"]: one cell, a single value.
  • loc["Tue":"Thu", [...]]: a label slice includes the end (Thursday is there), because there is no sensible "label just before Thu".

iloc: by position

byday = week.set_index("day")
print(byday.iloc[2])
print(byday.iloc[1:3])
print(byday.iloc[-1, 0])
cups        410
price        12
weather    cold
Name: Wed, dtype: object
     cups  price weather
day                     
Tue   280     10    rain
Wed   410     12    cold
450
  • iloc[2]: the third row (positions start at 0).
  • iloc[1:3]: positions 1 and 2; the end is excluded.
  • iloc[-1, 0]: last row, first column (Sunday's cups).

The trap: labels and positions drift apart

last2 = week.tail(2)
print(last2)
print(last2.iloc[0])
print(last2.loc[0])
   day  cups  price weather
5  Sat   390     12    cold
6  Sun   450     12    rain
day         Sat
cups        390
price        12
weather    cold
Name: 5, dtype: object
ValueError: 0 is not in range

The above exception was the direct cause of the following exception:

KeyError: 0

tail(2) keeps the original labels 5 and 6. iloc[0] (first position) is Saturday, but loc[0] asks for the label 0, which no longer exists. After you slice, filter or sort, labels and positions stop matching. Decide which one you mean, then choose loc or iloc on purpose.

One cell: at and iat

byday = week.set_index("day")
print(byday.at["Wed", "cups"])
print(byday.iat[2, 0])
410
410

at[row_label, column] and iat[row_pos, col_pos] only ever read or set one value, so they skip loc/iloc's general machinery: the light, quick way to get or set a single cell.

Changing a value: chained assignment fails

Tuesday really sold 290 cups. The obvious way chains two bracket steps: first the column, then the row. Using brackets one after another is chained indexing; setting a value through it is chained assignment.

week["cups"][1] = 290
print(week.head(3))
   day  cups  price weather
0  Mon   320     10   sunny
1  Tue   280     10    rain
2  Wed   410     12    cold
main.py:11: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment.
Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write).

Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step.

See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment
  week["cups"][1] = 290

(The warning is printed to the error stream, so in a terminal it may appear before or after the table.) Tuesday still says 280. The first step, week["cups"], produced a result that behaves as a copy; the second step changed that copy, which was then thrown away.

View, copy and Copy-on-Write

  • A view is like a window onto the original file: write on it and the original changes. A view shares the original's memory (the place its data is stored).
  • A copy is a photocopy: it has its own data, so changing it leaves the original alone.

In pandas before 3.0, some selections returned views and some returned copies, and it was hard to tell which. So pandas showed a SettingWithCopyWarning: "you may be setting a value on a copy".

pandas 3.0 (released 21 January 2026) made Copy-on-Write (CoW) the only behaviour. Its release notes say the result of any indexing operation "always behaves as if it were a copy", that chained assignment "will stop working", and that SettingWithCopyWarning "is removed", with defensive .copy() calls "no longer needed". "Copy-on-write" means pandas only really copies the data at the moment you change it, so plain reading stays fast and memory-cheap.

The fix: one step with loc (or at)

week.loc[1, "cups"] = 290
print(week.head(3))
   day  cups  price weather
0  Mon   320     10   sunny
1  Tue   290     10    rain
2  Wed   410     12    cold

Rule: to change values, put the row and the column in the same loc (or at) call on the left of =.

A part you take out is its own copy

weekend = week.iloc[5:7]
weekend.loc[5, "cups"] = 0
print(weekend)
print(week.tail(2))
   day  cups  price weather
5  Sat     0     12    cold
6  Sun   450     12    rain
   day  cups  price weather
5  Sat   390     12    cold
6  Sun   450     12    rain

Changing weekend never changes week. (In old pandas this exact code raised SettingWithCopyWarning.)

What is .copy() for now?

.copy() makes a real, separate copy straight away. Under Copy-on-Write you don't need it to avoid warnings, but it still states your intent clearly: "this is my own table and I'm going to change it".

weekend = week.iloc[5:7].copy()
weekend["cups"] = weekend["cups"] * 2
print(weekend)
   day  cups  price weather
5  Sat   780     12    cold
6  Sun   900     12    rain

Mini project: answer the accountant

byday = week.set_index("day")
print(byday.loc["Fri", "cups"] * byday.loc["Fri", "price"])
print(byday.loc["Mon":"Wed", "cups"].sum())
print(byday.iloc[-3:][["cups", "weather"]])
byday.loc["Sun", "price"] = 15
print(byday.loc["Sun"])
3500
1010
     cups weather
day              
Fri   350   sunny
Sat   390    cold
Sun   450    rain
cups        450
price        15
weather    rain
Name: Sun, dtype: object
  • Friday's revenue: 350 cups × ₹10 = ₹3,500.
  • Monday to Wednesday (label slice, end included): 320 + 280 + 410 = 1,010 cups.
  • iloc[-3:]: the last three rows; [["cups", "weather"]] picks two columns.
  • Sunday's price changed in one step with loc.

Common mistakes

Code Real error / symptom Why Fix
week["day", "cups"] KeyError: ('day', 'cups') Two names make one tuple key week[["day", "cups"]]
week[2] KeyError: 2 [] with one value means a column week.iloc[2] / week.loc[2]
week.tail(2).loc[0] KeyError: 0 Labels 5 and 6 were kept .iloc[0] for the first row
marks.iloc[5] on 5 rows IndexError: single positional indexer is out-of-bounds Positions run 0 to 4 Check len(df); use -1 for the last
marks.loc[:, "Maths"] KeyError: 'Maths' Names are case-sensitive Check df.columns
week["cups"][1] = 290 ChainedAssignmentError: A value is being set on a copy ... (nothing changes) The first step behaves as a copy week.loc[1, "cups"] = 290
Expecting loc["Tue":"Thu"] to stop before Thu (no error: Thu included) Label slices include the end Use iloc for end-excluded slices

Practice

All practice questions use this table:

import pandas as pd
marks = pd.DataFrame({
    "name": ["Asha", "Ravi", "Meena", "Kabir", "Zoya"],
    "maths": [81, 67, 92, 74, 88],
    "science": [77, 72, 95, 69, 90],
    "city": ["Pune", "Agra", "Pune", "Delhi", "Agra"],
})

1. Show only name and maths. Then, with name as the index, get Meena's science mark and the maths and science marks from Ravi to Kabir.

Solution
print(marks[["name", "maths"]])
m = marks.set_index("name")
print(m.loc["Meena", "science"])
print(m.loc["Ravi":"Kabir", ["maths", "science"]])
    name  maths
0   Asha     81
1   Ravi     67
2  Meena     92
3  Kabir     74
4   Zoya     88
95
       maths  science
name                 
Ravi      67       72
Meena     92       95
Kabir     74       69

2. With iloc, get rows 1 to 3 and the first two columns; then the last two rows; then the value in the last row, second column.

Solution
print(marks.iloc[1:4, 0:2])
print(marks.iloc[-2:])
print(marks.iat[4, 1])
    name  maths
1   Ravi     67
2  Meena     92
3  Kabir     74
    name  maths  science   city
3  Kabir     74       69  Delhi
4   Zoya     88       90   Agra
88

1:4 stops before position 4, so it gives positions 1, 2, 3.

3. top = marks.iloc[[2, 4]]. What do top.iloc[0]["name"] and top.loc[4, "name"] give?

Solution
top = marks.iloc[[2, 4]]
print(top)
print(top.iloc[0]["name"], top.loc[4, "name"])
    name  maths  science  city
2  Meena     92       95  Pune
4   Zoya     88       90  Agra
Meena Zoya

A list inside iloc picks those positions. top keeps labels 2 and 4: position 0 is Meena, label 4 is Zoya.

4. Ravi's maths should be 70 and Kabir moved to Noida. Fix both correctly.

Solution
marks.loc[1, "maths"] = 70
marks.at[3, "city"] = "Noida"
print(marks)
    name  maths  science   city
0   Asha     81       77   Pune
1   Ravi     70       72   Agra
2  Meena     92       95   Pune
3  Kabir     74       69  Noida
4   Zoya     88       90   Agra

5. Make your own copy of the Pune students' rows (positions 0 and 2), add 5 grace marks to maths in the copy, and show that marks is unchanged.

Solution
pune = marks.iloc[[0, 2]].copy()
pune["maths"] = pune["maths"] + 5
print(pune)
print(marks.loc[[0, 2], "maths"])
    name  maths  science  city
0   Asha     86       77  Pune
2  Meena     97       95  Pune
0    81
2    92
Name: maths, dtype: int64

(Run on the original table, before question 4's change.)

Quick quiz

s = week.set_index("day")
print(len(s.loc["Mon":"Wed"]),
      len(s.iloc[0:3]),
      len(s.iloc[0:2]))

A. 3 3 2 B. 2 3 2 C. 3 2 2 D. 2 2 2

Answer

A. loc["Mon":"Wed"] includes Wednesday → 3 rows. iloc[0:3] excludes position 3 → 3 rows. iloc[0:2] → 2 rows.

Interview questions

  1. Difference between loc and iloc? loc selects by index labels and column names, and label slices include the end; iloc selects by integer positions, and slices exclude the end.
  2. Why does df[0] raise KeyError on a DataFrame? A single value in [] is a column lookup; there's no column named 0. Use df.iloc[0] for the first row.
  3. df["a"] vs df[["a"]]? A Series vs a one-column DataFrame.
  4. When do loc and iloc give different rows for the same number? When the index labels aren't 0..n-1 in order, e.g. after filtering, sorting, tail, or with a custom index.
  5. What are at and iat? Fast accessors for a single cell, by label and by position.
  6. What is chained assignment and why doesn't it work in pandas 3? Setting through two indexing steps (df["a"][mask] = v). Under Copy-on-Write each step behaves as a copy, so the change never reaches df; pandas raises a ChainedAssignmentError warning. Use df.loc[mask, "a"] = v.
  7. What is Copy-on-Write? The pandas 3 model where every indexing result and every method result behaves as a copy; data is only physically copied when one side is modified.
  8. What was SettingWithCopyWarning? A pre-3.0 warning that you might be modifying a copy (a slice) instead of the original; removed in pandas 3.0 because the behaviour is now consistent.
  9. View vs copy? A view shares memory with the original (changes show through); a copy has its own data.
  10. Is .copy() still needed? Not to silence warnings; it's still useful to make intent explicit or to detach a large subset immediately.

Glossary

Term Meaning Example
Selecting / indexing Pointing at part of a table with code week["cups"]
[] on a DataFrame One name → a column; a slice → rows week["cups"], week[2:4]
Double brackets [[ ]] A list of column names inside []; returns a DataFrame week[["day", "cups"]]
Tuple key Two values with a comma make one tuple, one key ("day", "cups")
Label The name of a row in the index (or a column name) "Wed", 5
Position A row's place counted from 0 the 3rd row = position 2
loc Select by labels: df.loc[rows, cols] byday.loc["Wed", "cups"]
iloc Select by integer positions byday.iloc[2, 0]
Slice A range start:stop iloc[1:3], loc["Tue":"Thu"]
Inclusive / exclusive end Whether the slice's last value is kept loc inclusive, iloc exclusive
Negative position Counts from the end; -1 is last iloc[-1]
at / iat Get or set one cell by label / position at["Wed", "cups"]
tail(n) The last n rows, original labels kept week.tail(2)
Chained indexing Brackets one after another week["cups"][1]
Chained assignment Setting a value through chained indexing (never works in pandas 3) week["cups"][1] = 290
ChainedAssignmentError pandas 3's warning that a chained assignment changed only a copy see above
View A selection that shares the original's memory the window analogy
Copy A selection with its own separate data the photocopy analogy
Memory Where a program's data is stored while it runs RAM
Copy-on-Write (CoW) pandas 3 model: results behave as copies; data copied only when written default since 3.0
SettingWithCopyWarning Pre-3.0 warning about setting values on a possible copy; removed in 3.0 old tutorials
.copy() Make an explicit, separate copy week.iloc[5:7].copy()
Traceback The list of calls Python prints before an error hidden by sys.tracebacklimit = 0
IndexError A position outside the valid range iloc[5] on 5 rows

Summary

  • df["col"] → Series; df[["a", "b"]] → DataFrame. df[2] is a column lookup; df[2:4] slices rows.
  • loc = by label (slice end included); iloc = by position (slice end excluded, -1 = last). Both take [rows, columns].
  • After tail, filtering or sorting, labels ≠ positions: choose loc or iloc deliberately.
  • at / iat read or set a single cell.
  • Chained assignment (df["a"][1] = v) changes a copy and does nothing in pandas 3 (ChainedAssignmentError). Use df.loc[row, col] = v.
  • Copy-on-Write (pandas 3.0, 21 Jan 2026): every selection behaves as a copy, so changing a subset never changes the original; SettingWithCopyWarning is gone; .copy() now just states intent.
  • Next episode: pandas 5: filtering rows.