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.
We'll cover the following...
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.
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
maskthat returnsTruefor rows in theemployees_df...