Lesson 8: Filtering, Sorting & Selecting Columns

1. Selecting Columns

Choose only needed columns — saves memory & makes code clearer.

# Single column
df["age"]

# Multiple columns
df[["name", "age", "city"]]

# Drop columns
df.drop(["id", "notes"], axis=1)

Tip: avoid df.age (dot notation) if column name has spaces/special chars.

Exercise 1

How to select columns "feature1" and "target"?

2. Filtering Rows

Use boolean conditions — very powerful.

# Single condition
df[df["age"] > 30]

# Multiple (AND)
df[(df["age"] > 25) & (df["city"] == "NY")]

# OR
df[(df["age"] > 40) | (df["salary"] > 100000)]

# NOT
df[~df["gender"].isin(["M", "F"])]

Used in ML: filter outliers, select training subset.

Exercise 2

To get rows where score greater than 90 and city is LA:
df[(df["score"] 90) & (df["city"] "LA")]

Exercise 3

Which are correct filtering examples?
← Previous Lesson (7) Next Lesson (9) →