...

/

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
main.py
employees.csv
NAME,CITY,COUNTRY,HEIGHT,WEIGHT,ACCOUNT A,ACCOUNT B,TOTAL ACCOUNT
Kevin Hart,MELBOURNE,AUSTRALIA,57,134,2392,4342,6734
Judith Elliot,MANCHESTER,UNITED KINGDOM,61,167,4502,34334,38836
Lydia Carrasco,Oslo,Norway,56,119,,5505,8950
Jane Mattew,AMSTERDAM,NEDERLANDS,59,123,4346,9000,400
Von Gard,Berlin,GERMANYY,,127,7002,19002,26004
Juio Hernade,Mexico City,MEXICO,67,168,5000,4000,3452
Lydia Carrasco,Oslo,Norway,,119,3445,5505,8950
Judith Elliot,MANCHESTER,UNITED KINGDOM,61,,4500,2300,6800
Juio Hernade,Mexico City,MEXICO,67,168,5000,4000,3452
Judith Elliot,MANCHESTER,UNITED KINGDOM,61,167,4502,34334,38836
Lydia Carrasco,Oslo,Norway,56,119,,5505,8950

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