Boolean indexing in pandas dataframes with multiple conditions

Given a pandas dataframe, we have to use boolean indexing in it with multiple conditions. By Pranit Sharma Last updated : October 02, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

In programming, we sometimes use some specific values that only have two values, either True or False. These values are known as Boolean values. Boolean indexing in pandas is nothing but indexing the rows of the pandas DataFrame with their actual values (True or False) rather than naming them with a string or any integer value.

Problem statement

Suppose we are given a DataFrame with several columns and a new need to filter out some data by applying multiple conditions on this DataFrame. We are given a DataFrame containing the Name, Post, and salary columns of an employee. We will filter that data where the salary is greater than 20000.

Using/ Applying Boolean indexing in pandas dataframes with multiple conditions

For this purpose, just pass the condition inside the DataFrame index like df[(df['Salary'] <= 50000)] and assign the result to another DataFrame. In this code statement, df is an object of the DataFrame, Salary is the column name, and df2 is an object of another DataFrame. This will filter all of those records whose salary is less than or equal to 50,000.

Let us understand with the help of an example,

Python program to demonstrate the use of Boolean indexing in pandas dataframes with multiple conditions

# Importing pandas package
import pandas as pd

# Creating a dictionary

d = {
    'Name':["Ayushi", "Parth", "Sudhir", "Ganesh"],
    'Post': ["HR", "SDE", "Data-Analyst", "SDE"],
    'Salary':[40000, 50000, 80000, 52000]

# Creating a DataFrame
df = pd.DataFrame(d, index = [True, False, True, False])

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Applying conditions and filtering out data
df2 = df[(df['Salary'] <= 50000)]

# Display df2


The output of the above program is:

Example: Boolean indexing in pandas dataframes with multiple conditions

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.