Start studying
Module 6 · Data Wrangling Module contents
Module 6  ·  Card 10 / 14beginner

Filtering Rows

Filtering keeps only the rows of a DataFrame that meet a condition.

A comparison such as sales["price"] > 100 gives a Series of True and False, one value per row.

Putting that Series inside square brackets after the DataFrame keeps the rows marked True.

For example, sales[sales["region"] == "North"] keeps only the northern sales.

Conditions combine with & for and, | for or, and ~ for not, with each condition wrapped in its own brackets.

Learn in depth ↓
Module 06 · Data Wrangling · 14 concepts
In depth  /  15 lines

Filtering Rows, in more detail

Filtering is the most common step in data work, because most questions are about part of the data.

The mechanism is a boolean mask: a Series of True and False values, one for each row of the table.

pandas lines the mask up with the rows by index label and keeps the rows marked True.

Comparisons run on whole columns at once, so building a mask is vectorised and fast.

The & and | operators need brackets around each comparison, because they bind more tightly than > or ==.

Python's own and and or raise an error on a Series, since a whole column has no single truth value.

The isin method checks membership in a list, such as keeping rows whose region is North or South.

The between method keeps values within a range, and str.contains matches text inside a column.

A blank fails greater than, less than and equals tests, so a price filter quietly drops rows where the price is empty.

Checking the row count before and after a filter shows how much was removed.

The query method offers a shorter form, such as sales.query("price > 100 and qty > 5").

A filtered result can behave like a copy, so changes should go through loc on the original table.

Filtering early keeps every later step smaller and faster.

It is the pandas version of a SQL WHERE clause, and the logic carries straight across.

A filter that silently matches nothing is the usual trap, such as testing for north when the data says North.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst writes sales[sales["price"] > 100 & sales["qty"] > 5] and gets an error. What is wrong?
Data Wrangling

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close