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