In this article we will discuss different ways to select rows in DataFrame based on condition on single or multiple columns.

First let’s create a DataFrame,

Contents of DataFrame will be,

Now let’s select rows from this DataFrame based on conditions,

Select Rows based on value in column

Select rows in above DataFrame for which ‘Product’ column contains the value ‘Apples’,

It will return a DataFrame in which Column ‘Product‘ contains ‘Apples‘ only i.e.

How does that work internally ?

Applying condition on a DataFrame like this,

Will return a Series object of True & False i.e.

Series will contain True when condition is passed and False in other cases.

If we pass this series object to [] operator of DataFrame, then it will return a new DataFrame with only those rows that has True in the passed Series object i.e.

It will return a DataFrame in which Column passed series object had True entry i.e.

Select Rows based on any of the multiple values in column

Select rows in above DataFrame for which ‘Product‘ column contains either ‘GrapesorMangos‘ i.e

We have passed a list of product names in isin() function of DataFrame that will return True for each entry in ‘Product‘ column that matches with any entry in given list.
Therefore, it will return a DataFrame in which Column ‘Product‘ contains either ‘Grapes‘ or ‘Mangos‘ only i.e.

Select DataFrame Rows Based on multiple conditions on columns

Select rows in above DataFrame for which ‘Sale’ column contains Values greater than 30 & less than 33 i.e.

It will return following DataFrame object in which Sales column  contains value between 31 to 32,

Complete example is as follows,

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.