Pandas DataFrame isin() | Explained

In Python, a Dataframe is a 2-dimensional representation of data in the form of rows and columns. Python provides a Pandas library that can be used to access the Dataframe. Inside that Pandas library, a function named β€œdataframe.isin()” is utilized that especially checks or filters the specific data.

This write-up will give you an in-depth understanding of Pandas β€œdataframe.isin()function with numerous examples. This Python guide covers the following topics:

Let’s start the guide!

What is Pandas DataFrame isin() Function?

In Python, the pandas β€œdataframe.isin()” function is used to check whether the data frame contains the specified value. By doing so, the data can be filtered effectively. The syntax of the Pandas β€œdataframe.isin()” function is shown below:

Syntax:

dataframe.isin(values)

In the above syntax, the parameter β€œvalues” takes any iterable, series, DataFrame, or dictionary. The β€œdataframe.isin()” function returns true or false for the respective values of the Dataframe, as it replaces the boolean value in the original Dataframe.

Let’s have a look at the examples given below to implement this function practically.

Example 1: Check the Specified Value Using Pandas dataframe.isin()

In the example given below, the β€œdataframe.isin()” function is used along withβ€œrange()” iterable to check the specified value in the data frame.

Code:

import pandas as pd

#create dataframe
dataframe = pd.DataFrame({'Name': ['Alex','Lily'],'Position': [1, 5], 'Marks': [55, 70]})

#check the data frame marks are in the range(50,60)
result = dataframe.isin(range(1,60))

print('DataFrame\n',dataframe)
print('\nDataFrame are present in range: \n',result)

In the above code:

  • The β€œpandas” library is imported as β€œpd”.
  • The β€œpd.DatafFame()” is used to create a Dataframe with column names β€œName”, β€œPosition” and β€œMarks”. All the columns contain two values, each at index β€œ0” and β€œ1”.
  • The β€œdataframe.isin()” function accepts the β€œrange()” iterable as an argument.
  • The range values of the Data Frame will be compared with the range specified i.e. (1, 60). It will embed true or false instead of the original values of the Dataframe.
  • It retrieves the boolean value β€œTrue” if the data frame contains a range-specific value.

Output:

First, a data frame is created, and then each element of the data frame is checked by the range value using the β€œdataframe.isin()” function.

Example 2: Comparing the Value of One DataFrame With Another Using Pandas dataframe.isin()

In the example given below, the β€œdataframe.isin()” function checks the value of one data frame with another.

Code:

import pandas as pd

#create first dataframe
first_dataframe = pd.DataFrame({'a': [1, 3], 'b': ['Alex', 10]})

#create second dataframe
second_dataframe = pd.DataFrame({'a': [2, 3], 'b': ['Alex', 3]})

result = first_dataframe.isin(second_dataframe)

print('First DataFrame: \n',first_dataframe)
print('\nSecond DataFrame: \n',second_dataframe)
print('\nChecking Value of DataFrame:\n',result)

In the above code:

  • The β€œpandas” library is imported as β€œpd”.
  • The β€œpd.DataFrame()” is used to create two data frames and stored in variables named β€œfirst_dataframe” and β€œsecond_dataframe”.
  • The β€œfirst_dataframe.isin()” takes the value of the second data frame as an argument and returns the boolean value β€œTrue” if elements are matched else, β€œFalse”.
  • By using this function, all the elements of one data frame can be easily filtered out.

Output:

This output returns true if the data frame element is matched. Otherwise, it returns false.

Example 3: Checking the Dictionary Value in DataFrame Using Pandas dataframe.isin()

In the code given below, the pandas β€œdataframe.isin()” function checks the contained value of the dictionary in the given data frame.

Code:

import pandas as pd

#create dataframe
dataframe = pd.DataFrame({'Name': ['Alex','Lily'],'Position': [1, 5], 'Marks': [55, 70]})

#create dictionary
dictionary = {'Position': [1, 5]}

#DataFrame.isin(dict)
result = dataframe.isin(dictionary)

print('DataFrame Value: \n',dataframe)
print('\nDictionary Value: \n',dictionary)
print('\nChecking Dictionary Value in DataFrame: \n',result)

In the above code:

  • A Dataframe is created and stored in a variable named β€œdataframe”.
  • The dictionary value is initialized with the same key name also present in the given β€œDataFrame”.
  • The β€œdataframe.isin()” function takes the value of β€œdictionary” as an argument and matches each element value of the data frame. If the element value of the dictionary is matched, then this function returns a β€œTrue” value.

Output:

In the above output, the Dataframe containing the dictionary value returns the β€œTrue” value using β€œdataframe.isin()” function.

Note: The β€œisin()” function is also used to check the other iterable objects, such as list, series, tuple, etc., in the given data frame using the same method.

The guide is now complete!

Conclusion

In Python, the function β€œdataframe.isin()” is used to determine whether every element/item in a data frame is included in a value. The values can be a list, dictionary, Dataframe, series, range, etc. The β€œdataframe.isin()” takes the value as an argument and returns β€œTrue” or β€œFalse” if each element is found in the data frame or not. In this guide, we take β€œdictionary”, β€œrange”, and β€œdata frame” as an argument in the β€œdataframe.isin()” function and check the contained value in the given data frame.

This python guide covered pandas’ β€œdataframe.isin()” function in detail with multiple examples.