pandas indexing and selection cheat sheet
loc, iloc, boolean masks, query() and column selection without SettingWithCopy warnings.
Selecting
Columns
df["name"] # Series df[["name", "dept"]] # DataFrame
loc vs iloc
df.loc[df["dept"] == "eng", ["name", "salary"]] # by label df.iloc[0:3, 0:2] # by position
Filtering
Boolean masks
df[(df["salary"] > 100) & (df["country"] == "UK")]
Use & and |, and wrap each condition in parentheses.
isin and query
df[df["country"].isin(["UK", "USA"])]
df.query("salary > 100 and dept == 'eng'")Safe assignment
df.loc[df["dept"] == "eng", "bonus"] = 10
Try it live
python · editable
loading editor…
Practice it
learn pandas step by step →More cheat sheets
Ready for interview-level questions? Pro unlocks the full company problem set.