Selecting rows and columns with loc, iloc and filters
How loc and iloc differ, why slice ends behave differently, and how to write combined filters that do not break.
Most pandas screening questions about selection come down to one idea: loc works with labels and iloc works with positions. If you can predict the output of a slice before running it, you will get these questions right.
loc uses labels
loc takes row labels first and column labels second. With the default index of 0, 1, 2 and so on, labels and positions look the same, which is why people confuse the two.
df.loc[0:2, ["name", "city"]] # rows labelled 0, 1 and 2
df.loc[df["city"] == "Lagos", "sales"]A loc slice includes the end label. So df.loc[0:2] returns three rows. If the index holds dates or employee codes, you slice with those values, such as df.loc["2024-01-01":"2024-01-31"] on a date index.
iloc uses positions
iloc behaves like Python list slicing. The end position is excluded, and negative numbers count from the end.
df.iloc[0:2] # first two rows
df.iloc[-1] # last row
df.iloc[:, 0:3] # first three columnsA common mistake is passing a column name to iloc. It only accepts integers, slices of integers or boolean arrays, so df.iloc[0, "name"] fails.
Filtering with conditions
A condition such as df["age"] > 30 returns a True/False Series, and passing it inside brackets keeps the True rows. To combine conditions:
- Use
&for and,|for or, and~for not. - Put each condition in its own parentheses, because & is evaluated before comparison operators.
- Use
isinfor several allowed values:df[df["city"].isin(["Pune", "Nairobi"])].
Writing and instead of & raises a ValueError about the truth value of a Series being ambiguous. Interviewers like this error because it shows whether you have actually written filters yourself.
Changing values safely
To update filtered rows, do it in one loc call:
df.loc[df["age"] < 0, "age"] = 0Chained indexing such as df[df["age"] < 0]["age"] = 0 may change a temporary copy instead of df.
Habit to build: before running a selection, say out loud whether you are using labels or positions, and how many rows you expect. Then check with .shape.
More in Data and AI