Open In App

Python | Pandas DataFrame.isin()

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the Pandas DataFrame.isin() method provided by the Pandas library in Python. Python is widely recognized for its proficiency in data analysis, largely attributed to its exceptional ecosystem of data-centric packages. Among these, Pandas stands out as an essential tool that significantly simplifies tasks related to data import and analysis.

Pandas DataFrame.isin() Syntax

Syntax: DataFrame.isin(values)

Parameters: values: iterable, Series, List, Tuple, DataFrame or dictionary to check in the caller Series/Data Frame.

Return Type: DataFrame of Boolean of Dimension.

To download the CSV file used, Click Here.

isin() Function in Pandas Examples

The DataFrame.isin() method in Pandas is a powerful tool for filtering and selecting data within a DataFrame based on specified conditions. It allows you to create boolean masks to identify rows where the values in one or more columns match certain criteria. Let’s delve into the details of the isin() method

  • Single Parameter Filtering
  • Multiple Parameter Filtering

Single Parameter filtering Using Pandas DataFrame.isin()

In the following example, Rows are checked and a boolean series is returned which is True wherever Gender=”Male”. Then the series is passed to the data frame to see new filtered data frame.

Python3




# importing pandas package
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("/content/employees (2).csv")
 
# creating a bool series from isin()
new = data["Gender"].isin(["Male"])
 
# displaying data with gender = male only
data[new]
print(data.head())


Output

As shown in the output image, only Rows having gender = “Male” are returned.

Multiple parameter Filtering Using Pandas DataFrame.isin()

In the following example, the data frame is filtered on the basis of Gender as well as Team. Rows having Gender=”Female” and Team=”Engineering”, “Distribution” or “Finance” are returned.

Python3




# importing pandas package
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("/content/employees (2).csv")
 
# creating filters of bool series from isin()
filter1 = data["Gender"].isin(["Female"])
filter2 = data["Team"].isin(["Engineering", "Distribution", "Finance" ])
 
# displaying data with both filter applied and mandatory
data[filter1 & filter2]
 
print(data.head())


Output

As shown in the output image, Rows having Gender=”Female” and Team=”Engineering”, “Distribution” or “Finance” are returned.



Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads