Pandas is an open-source Python library. It is widely used throughout the data science industry. It is a fast and very powerful Python tool to perform data analysis. Pandas provide commands to read, filter, inspect, manipulate, analyze, and plot data.

Pandas dataframe is a tabular structure. It is a two-dimensional data structure that consists data in the form of rows and columns. This library is used to group, aggregate, clean, and filter data.

Filtering data is a preliminary step for any data science and machine learning application. It allows us to create subsets from the original dataset by forming smaller dataframes. 

Let’s take a look at a few different ways to filter and select rows in a Pandas dataframe based on multiple conditions.To start we’re going to create a simple dataframe in Python:

import pandas as pd

df=pd.DataFrame({
    'name':['GT','CSK','LSG','RCB','MI','RR','KKR','PK','DC','SH'],
    'Mat':[13,13,13,13,13,13,13,13,13,13],
    'Won':[9,7,7,7,7,6,6,6,5,4],
    'Lost':[4,5,5,6,6,7,7,7,8,9],
    'Pts':[18,15,15,14,14,12,12,12,10,8],
    'NRR':[0.835,0.381,0.304,0.180,-0.128,0.140,-0.256,-0.308,-0.572,-0.559]
})
Pandas Filter Dataframe

Filter Pandas dataframe using relational conditions

This method is often used to select rows based on particular column values. Comparison operators are used to write these conditions.

Single Condition:

df[(df.NRR>0)]

Multiple Conditions:

We use the ampersand (&) operator and the pipe (|) operator, for “and” and “or” logical operators respectively.

Syntax: df [ (df[‘‘column name 1' ]==’column value’ ) & (df[‘‘column name 2' ]==’column value’ )]

df[(df['NRR']>0)| (df['Pts']>14)]
Pandas Filter Dataframe or and

Add each condition you want to be included in the filtered result and concatenate them with the “&” and “|” operators. 

Using Query Function

The query function takes a query parameter in the form of a condition. It evaluates whether the condition is True or False. In simple words, it is used to query the columns using a boolean expression. This function can be used for both single or multiple conditions. It returns a new dataframe.

df.query('NRR>0 and Pts>14')
Pandas Filter Dataframe Using Query

If we have to update the changes in the original dataframe, use inplace=True as an argument in the query function.

Related Post

Discretization, Binning, and Count in Pandas Column.

Create Pandas DataFrame with Random Data

Query list-type column in Pandas.