pandas Cheatsheet
Selecting Data
Use this pandas reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Column Selection
# Single column → Series df["col"] # Multiple columns → DataFrame df[["col1", "col2"]] # Attribute access (only works when name is a valid Python identifier, no spaces/dots) df.col # All columns except one df.drop(columns=["col"]) # Columns matching a pattern df.filter(like="score") # columns containing "score" df.filter(regex=r"^val_") # columns matching regex
Row Selection by Position — .iloc
.iloc uses integer positions (0-based), like NumPy.
df.iloc[0] # first row → Series df.iloc[-1] # last row → Series df.iloc[2:5] # rows 2, 3, 4 → DataFrame df.iloc[[0, 2, 4]] # specific rows # Row + column position df.iloc[0, 1] # scalar at row 0, col 1 df.iloc[0:3, 0:2] # slice rows and columns df.iloc[[0, 1], [2, 3]] # specific rows and cols df.iloc[:, 2] # all rows, column at position 2 df.iloc[:, -1] # last column
Row Selection by Label — .loc
.loc uses index labels and column names. Slices are inclusive on both ends.
df.loc["row_a"] # row by label → Series df.loc[["row_a", "row_c"]] # multiple rows df.loc["row_a":"row_c"] # inclusive label slice # Label-based row + column df.loc["row_a", "col1"] # scalar df.loc["row_a", ["col1", "col2"]] # row, specific cols df.loc["row_a":"row_c", "col1":"col3"] # both sliced df.loc[:, "col1"] # all rows, one column # With boolean mask mask = df["age"] > 30 df.loc[mask, "name"]
Scalar Access — .at and .iat
Faster than .loc/.iloc for a single cell.
df.at["row_label", "col_name"] # label-based scalar df.iat[2, 3] # position-based scalar # Assignment df.at["row_a", "score"] = 99 df.iat[0, 1] = 42
Boolean / Mask-Based Selection
mask = df["age"] > 30 df[mask] df.loc[mask] # Multiple conditions — use & | ~ with parentheses df[(df["age"] > 25) & (df["city"] == "NYC")] df[(df["score"] < 50) | (df["score"] > 90)] df[~(df["col"].isna())] # .query() — readable string expressions df.query("age > 30") df.query("city == 'NYC' and age > 25") df.query("score.between(50, 90)") # method calls in query threshold = 30 df.query("age > @threshold") # reference local variable with @
.isin() and .between()
df[df["city"].isin(["NYC", "LA", "Chicago"])] df[~df["status"].isin(["inactive", "deleted"])] df[df["score"].between(60, 90)] # inclusive by default df[df["score"].between(60, 90, inclusive="left")] # "left" | "right" | "both" | "neither"
MultiIndex Selection
# Create a MultiIndex DataFrame df = df.set_index(["year", "month"]) # .loc with tuples df.loc[(2024, 1)] # exact level values df.loc[(2024, slice(None))] # all months of 2024 df.loc[2024] # outer level only # pd.IndexSlice for cleaner syntax idx = pd.IndexSlice df.loc[idx[2024, 1:6], :] # 2024, months 1-6 df.loc[idx[:, 3], "value"] # all years, month 3 # .xs — cross-section df.xs(2024, level="year") df.xs((2024, 6), level=["year", "month"])
Selecting by Dtype
df.select_dtypes(include="number") # all numeric cols df.select_dtypes(include=["int64", "float64"]) df.select_dtypes(exclude=["object", "category"]) df.select_dtypes(include="datetime")
.where() and .mask()
# Keep values where condition is True; replace False with NaN (or other) df.where(df > 0) df.where(df > 0, other=-1) # replace False with -1 # Inverse: keep where condition is False df.mask(df < 0) df.mask(df < 0, other=0)
First / Last / nth Row
df.head(10) # first N rows df.tail(10) # last N rows df.first("7D") # first 7 days (DatetimeIndex) df.last("30D") # last 30 days df.nth(2) # 3rd row (works on GroupBy too) df.sample(n=5) # random 5 rows df.sample(frac=0.1) # random 10% df.sample(n=5, random_state=42)
Chained Selection Warning
Avoid
df["col"][mask] = value. Usedf.loc[mask, "col"] = valueinstead to prevent theSettingWithCopyWarningand ensure writes take effect on the original DataFrame.