Search⌘ K
AI Features

Performing Filtering

Explore multiple techniques to filter pandas DataFrames in Python, including boolean indexing, loc, query, and isin methods. Learn how to select rows based on conditions, such as filtering by country names, to prepare data effectively for analysis.

Method 1: Boolean indexing

We use boolean indexing to filter a DataFrame by creating a boolean array that specifies the criteria we want to use to select the rows. Then, we use that boolean array to index the DataFrame and select only the rows that meet the criteria.

In the following example, we'll use boolean indexing to select only the rows in the DataFrame where the value in the COUNTRY column is UNITED KINGDOM.

C++

Let’s review the code line by line:

  • Lines 1–3: We import the pandas library, load the dataset, and set to display all columns in the DataFrame.

  • Line 4: We create a boolean array called mask that returns True for rows in the employees_df ...