Filtering by Condition
Learn how to filter values in a DataFrame based on a condition or a set of conditions.
We'll cover the following
How to filter a DataFrame
by condition
Recall the sales
we’ve been using. It contains products that belong to one of six different product groups. Suppose we’re interested in only one product group and would like to filter products that belong to this group.
The following line of code selects the products that belong to product group PG2
. Remember that each row in the sales represents a product.
sales_filtered = sales[sales.product_group == "PG2"]
The following line of code does the same operation.
sales_filtered = sales[sales["product_group"] == "PG2"]
We can use any of the options above, unless there’s a space in the column name. In such cases, the first option won’t work.
We can also filter a DataFrame
based on numerical values. For instance, the following line of code selects the products with a price higher than 100.
sales_filtered = sales[sales["price"] > 100]
The operators we can use to create conditions are:
==
: equal!=
: not equal>
: greater than>=
: greater than or equal to<
: less than<=
: less than or equal to
Get hands-on with 1400+ tech skills courses.