+

Python | Pandas DataFrame.isin ()

The Pandas isin() method is used to filter data frames. The isin () method helps in selecting rows with a specific (or multiple) value in a specific column.

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 you are using, press here.

Example # 1: Filtering one parameter
In the following The example tests strings and returns a Boolean series that is True wherever gender = "male". The series is then passed into the dataframe to see the new filtered dataframe.

# import pandas package

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "employees.csv" )

 
# create a bool series from isin ()

new = data [ "Gender" ]. isin ([ "Male" ])

  
# display data with gender = men only
data [ new]

Output:
As shown in the output image, only lines with gender = male are returned.

Example # 2: Filtering Multiple Parameters
In the following example, a data frame is filtered by both gender and team. Rows with gender = "Female" and "Team" = "Engineering", "Distribution" or "Finance" are returned.

# import pandas package

import pandas as pd

  
# create data frame from CSV file

data = pd.read_csv ( "employees.csv" )

 
# create bool series filters from isin ()

filter1 = data [ "Gender" ]. isin ([ "Female" ])

filter2 = data [ "Team" ]. isin ([ "Engineering" , "Distribution" , " Finance " ])

  
# displaying data with filter applied and required
data [filter1 & amp; filter2]

Output:
As shown in the output image, rows having gender = "Female" and "Team" = "Engineering", "Allocation" or "Finance" are returned.

Get Solution for free from DataCamp guru