top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Pandas Techniques for Data Filtering

Pandas is one of the most widely used packages in Data Science. In this article, we will go through one of the most important techniques in pandas which is data filtering. Data Filtering is one of the data manipulations techniques that gives us the capability to focus on a certain subset of the data especially when dealing with big-sized datasets.

For this article, we will get our data from Kaggle.

At first, we will import our data that is stored as a CSV file like this:

import pandas as pd

let's see the first five rows of our data.


that's considered part of our data.

let's see our first way to filter the data. Now I am interested in getting the records that contain "Master" value from the Education column, we can do that by the following:


That was good but that was based on one condition. We want to filter based on two conditions:


Another example:


In the previous examples, we used two conditions to filter. Specifically, we used at the first example the symbol " | " that means or, to include both the "Master" or "PhD" values from the Education column that means that if the record has the value "PhD" or "Master" under the attribute Education so it will be included in the result but other values will not be included.

In the second example, we used the symbol & that means and, to view only the records that its Education attribute has the value "Master" and its Marital status attribute has the value "Married".

Another quick way to reverse the conditions that you wrote is by using Tilde ~.


This will result in all values except the "Master" value in the Education column.

Note that what we did in the previous examples also can be applied if we add three or more conditions.


Another useful method that is used frequently is the query method and it is easy to use as you do not need to mention the name of the dataframe every time when you specify the columns. its syntax is like this:

df.query('Education =="Master"& Marital_Status=="Single"')


In addition to the above, there is an important method and it is easy to grasp which is eq() and you can use it with any columns like the following example:



Let's see a different method to filter based on some values we provide, let's see this first in action, and then we will illustrate it:


Here we created a list that contains different years and then we used the .isin() method to include in the final resulting dataframe in the Year Birth column only the values that are also in the years' list that we created before.


One of the most important ways that are used in the selection of a certain subset of the data and also used for filtering is loc and iloc. we will look at some examples for how we should use them and make a brief distinction between them let's take some examples:


This example showed us how can we use loc to filter but this was similar to what we did in the previous ways for filtering, loc and iloc are helpful for subsetting the data based on some conditions in addition to that it gives us the ability to slice the columns according to our findings, We want to clarify that by some examples:


Here we filter the data based on two conditions and limit the resulting columns into just 4 columns.


In this example, we used iloc which differs from loc in some matters. I believe that we need to make a little comparison between them.

iloc is integer index-based so it accepts integers and not the name of the columns, which means that we have to specify rows and columns by their integer index. Also, the stop index in the slicing is excluded like what we saw before.

On the other hand, loc is label-based so it accepts the names of the columns, which means that we have to specify the name of the rows and columns, Also, if you use slicing the stop index is included.

This table summarizes some differences between them.


Also, you can combine string functions with pandas to give you more tools in your toolkit. An example of that is the following:


Here we used the string function to get only the records in which the Education column values start with the letter "M" which in our case it is the Master value.

You can do this with other functions like the following.

let's create small data like this:

             'Job':['Data Scientist','Accountant','Doctor','Engineer','Web Developer','Project Manager','Marketing Specialist','Teacher']}


Here we got only the names that its length is greater than five elements in length.

Also, we can use contains() like this:


Hope that was helpful, For more information and examples check the documentation. And for other Resources regarding this article: here.

Link for GitHub repo here.


That was part of Data insight's Data Scientist program.


Recent Posts

See All


bottom of page