0Pricing
Data Science Academy · Lesson

Filter Rows With Conditions

Boolean masks on a DataFrame.

Filter Rows With Conditions is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Columns to Rows

Picking columns trims width; filtering rows trims length. Now you keep only the records that match a condition you care about. 🔍

A Condition Makes a Mask

Write a comparison on a column and pandas returns a boolean mask: one True or False per row, telling you which records pass.

mask = df["age"] > 30
print(mask.head())

Apply the Mask

Put the mask inside brackets and pandas keeps only the True rows. The result is a smaller DataFrame with the same columns.

adults = df[df["age"] > 30]

Equality Filters

Use double equals to match a value exactly. Remember that a single equals would try to assign, not compare.

df[df["city"] == "Paris"]

Not Equal and Comparisons

You can filter with the full set of comparisons: not-equal, greater-equal, and less-equal all work on a column.

df[df["score"] >= 80]
df[df["city"] != "Paris"]

Filter on Text

String columns support a str accessor. Use str.contains to keep rows whose text includes a pattern.

df[df["email"].str.contains("@gmail")]

Filter With between

For a numeric range, between reads more clearly than two separate comparisons and includes both ends by default.

df[df["age"].between(18, 65)]

Keep Selected Columns Too

Combine row filtering with loc to grab matching rows and chosen columns in one readable line.

df.loc[df["age"] > 30, ["name", "age"]]

The Mask Is Reusable

A mask is just a Series, so you can store it and reuse it across different selections without rewriting the condition.

senior = df["age"] > 60
df[senior]
df.loc[senior, "name"]

Count the Matches

Because True counts as one, calling sum on a mask tells you how many rows pass before you even filter.

(df["age"] > 30).sum()

Filtering Does Not Change df

Filtering returns a new DataFrame and leaves the original untouched. Assign the result to a variable to keep it.

young = df[df["age"] < 30]
# df itself is unchanged

Quick Check

Recall how a comparison on a column behaves.

Recap: Filtering Rows

A condition builds a boolean mask, brackets apply it, and loc adds column selection. You can now keep exactly the records that matter. ✅

Frequently asked questions

Is the “Filter Rows With Conditions” lesson free?

Yes — the full text of “Filter Rows With Conditions” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Filter Rows With Conditions”?

Boolean masks on a DataFrame. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Filter Rows With Conditions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Pick Columns by Name
  2. Filter Rows With Conditions
  3. Combine Filters With AND and OR
  4. query and isin for Clean Filters
← Back to Data Science Academy