...

/

Performing Filtering

Performing Filtering

Learn how to filter data using different built-in methods of Python.

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.

Press + to interact
C++
Files

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