Search⌘ K
AI Features

Filtering by Condition

Explore how to filter DataFrames based on single or multiple conditions using comparison operators, logical AND/OR, and the isin method in Python Pandas. Understand how to include or exclude data with the not operator to refine your data analysis.

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"]
...