Introduction
Almost every analysis asks for part of a table: the rows from one city, the customers over 30, the first ten orders, two columns of five hundred. pandas has three tools for this. loc selects by label, iloc by position, and boolean masks by condition. They look alike and behave differently in one important way, so it pays to learn them side by side.
How it works
loc takes row labels and, optionally, column labels. iloc takes positions, counting from 0:
df = pd.DataFrame({
"name": ["Ada", "Rahim", "Mei", "Omar"],
"age": [36, 24, 29, 41],
"city": ["London", "Dhaka", "Tokyo", "Dhaka"],
}, index=["a", "b", "c", "d"])
df.loc["b", "name"] # 'Rahim'
df.loc["b":"c", "name"].tolist() # ['Rahim', 'Mei']: both ends included
df.iloc[1:3]["name"].tolist() # ['Rahim', 'Mei']: the end is excluded
df.iloc[0, 1] # 36: row 0, column 1
The difference in slices is deliberate. With positions, Python's rule "end excluded" makes iloc[0:3] exactly three rows. With labels there is often no "next label" to stop before, so a loc slice includes both ends: df.loc["b":"c"] means from b to c. With the default index 0, 1, 2, ... the labels are numbers too, and df.loc[0:2] gives three rows while df.iloc[0:2] gives two.
A boolean mask is a Series of True and False, one value per row, usually made by comparing a column: df["age"] > 25. Putting a mask in brackets keeps the rows where it is True. Conditions are combined with the element-wise operators & (and), | (or) and ~ (not), and each condition needs its own parentheses, because & binds more tightly than >:
mask = (df["age"] > 25) & (df["city"] == "Dhaka")
df[mask]["name"].tolist() # ['Omar']
df[~(df["city"] == "Dhaka")]["name"].tolist() # ['Ada', 'Mei']
df.loc[df["age"] < 30, "name"].tolist() # ['Rahim', 'Mei']
Python's and and or do not work here: they need one True or False for the whole Series, and pandas raises "ValueError: The truth value of a Series is ambiguous".
Selections can also be assigned to. df.loc[df["city"] == "Dhaka", "age"] = 0 changes the age only in the matching rows. Use loc with both parts for this; writing df[mask]["age"] = 0 changes a temporary copy, and pandas warns about it.
Example
# loc (labels, both ends included), iloc (positions, end excluded) and boolean masks.
df = {
"name": ["Ada", "Rahim", "Mei", "Omar"],
"age": [36, 24, 29, 41],
"city": ["London", "Dhaka", "Tokyo", "Dhaka"],
}
index = ["a", "b", "c", "d"] # the row labels, like df.index
def loc(start, stop): # df.loc["b":"c"]
i, j = index.index(start), index.index(stop)
return index[i : j + 1]
def iloc(start, stop): # df.iloc[1:3]
return index[start:stop]
print("loc b:c ->", loc("b", "c"))
print("iloc 1:3 ->", iloc(1, 3))
# df[(df["age"] > 25) & (df["city"] == "Dhaka")]
mask = [(a > 25) and (c == "Dhaka") for a, c in zip(df["age"], df["city"])]
print("mask:", mask)
print("rows:", [label for label, keep in zip(index, mask) if keep])
# df.loc[df["age"] < 30, "name"]
print("under 30:", [n for n, a in zip(df["name"], df["age"]) if a < 30])Line by line
- The table has four rows labelled a to d.
loc("b", "c")finds both labels and keeps everything from the first to the second, inclusive.iloc(1, 3)slices positions 1 and 2, excluding 3.- The mask combines two conditions for each row; only Omar is over 25 and in Dhaka.
- The last line selects one column for the rows of a second mask, as
df.loc[mask, "name"]does.
Expected result
loc b:c -> ['b', 'c']
iloc 1:3 -> ['b', 'c']
mask: [False, False, False, True]
rows: ['d']
under 30: ['Rahim', 'Mei']
In the real world
Filtering is the most common step in data work: orders from the last month, customers in one region, sensor readings above a threshold, rows with a valid email. Dashboards often build their masks from the user's choices, combining several conditions with &. Assigning through loc is how analysts fix known bad values, such as setting impossible ages to missing.
Common mistakes
- Using and, or or not between conditions instead of
&,|and~. - Leaving out the parentheses around each condition, which changes what
&applies to. - Expecting loc slices to exclude the end, or iloc slices to include it.
- Assigning through chained brackets such as
df[mask]["age"] = 0, which may change a copy.
Best practices
- Use
locfor labels and conditions andilocfor positions, never plain brackets for rows. - Name complex masks before using them:
is_adult = df["age"] >= 18. - Assign with
df.loc[mask, "col"] = valuein a single step.
Summary
loc selects by label and includes both ends of a slice; iloc selects by position and excludes the end. Boolean masks keep the rows where a condition is True, combined with &, | and ~ and with each condition in parentheses. Assigning through df.loc[mask, column] changes only the selected rows.
Quick check
Key takeaways
- loc uses labels and includes both ends of a slice; iloc uses positions and excludes the end.
- Combine conditions with & and |, each condition in parentheses, and negate with ~.
- Assign through df.loc[mask, "col"] to change only the selected rows.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.