Conditional Filtering Using Pandas In Python

Filed Under: Pandas
Conditional Filtering Using Pandas In Python

Pandas need no introduction. Being a robust library for data manipulation and analysis, it is a lifeline for many data scientists and analysts. When you are working with data, you need to filter out certain values based on particular conditions. Yes, filtering can include a simple one-line code and also complex multiple conditions. In this article, let’s discuss conditional filtering using pandas in python. 

We will discuss multiple filtering methods offered by pandas. Let’s dive deep into each of those methods for conditional filtering.


Introduction to Conditional Filtering

  • The word may seem simple, but it’s not. When you are working with a large amount of data, you can use this method to filter required data from the large mess.
  • It is very useful in deselecting irrelevant values from your large dataset so that the left-out data can properly answer your questions.
  • But, make sure that left out data is not ignored. It may not answer your questions at that time, but certainly, it will answer some questions which will tell you a brief story.
  • A simple example of conditional filtering is – If you are working on a Covid dataset, you might need to filter only specific values such as age, sex, country, etc. Then you can condition to filter the data based on the requirements.

1. Eval Function

The eval functions offered by pandas will evaluate the strings and do the filtering over the data. You can observe an example below for a better understanding.

We will be working with coffee sales data for this whole tutorial. You can see a glimpse of the data here.

Coffeesales data
#Eval

import pandas as pd
df = pd.read_csv('coffeesales.csv')
df.head(5)

df[df.eval("sales >150 & product == 'Green Tea'")]
Eval function

In this –

  • We have asked for sales of a product ‘Green Tea’ which are above 150 in all regions. This will enable us to decide on the best regions and markets for Green Tea.

2. Query Function

The query function is another pandas function that helps us in filtering. It will take input as strings and then converts it as a condition to filter out the data.

This example can make you feel comfortable about this function.

#query

df.query("market == 'Wholesale' & net_profit > 500")
query

Here you can observe that the company is getting good profits from the Wholesale market in the East region.


3. IsIn Function

The pandas Isin function will search for the values in the rows which match the list. It is one of the simplest methods to filter out the data.

#isin

df[df['product_category'].isin(['Coffee','Espresso'])]
conditional filtering

Here, you can observe that we got all the values with respect to coffee and Espresso.

Just like this, you can use the pandas isin function for the conditional filtering of your data.


4. Single Condition

Here, let’s see how we can use single conditions to filter out the data we want from our dataset.

#single condition

data_2 = data[data['inventory'] > 500 ]
data_2.head(5)

conditional filtering

This is much simple I guess.


5. Multiple Conditions

I feel the single condition is much simple and straight forwarded as they do a single thing precisely. Now, let’s see how multiple conditions work.

#multiple conditions

data_3 = data[(data['net_profit'] > 50) & (data['sales'] > 200)]
data_3.head(5)
conditional filtering

This is some information from a single line of code. Here the complexity increases in the code as we will go with multiple conditions.

But, you will find it easy upon practicing.


Conditional Filtering – Conclusion

Conditional filtering using pandas is a widely used method to filter out the data based on certain conditions. The condition can be one or many based on the requirement. We have discussed 5 pandas functions that we can use for conditional filtering of the data. I hope you found this helpful. That’s all for now. Happy Python!!!

More read: Pandas filtering methods

close
Generic selectors
Exact matches only
Search in title
Search in content