Pandas DataFrame isin()

Pandas DataFrame isin()

DataFrame.isin(values) checks whether each element in the DataFrame is contained in values.

Syntax

DataFrame.isin(values)

where values could be Iterable, DataFrame, Series or dict.

isin() returns DataFrame of booleans showing whether each element in the DataFrame is contained in values.

Examples

1. DataFrame.isin() with iterable

In this example, we will apply DataFrame.isin() with a range i.e., Iterable. The values of the DataFrame that match the values in the range output True while other output False in the respective index of the DataFrame.

Python Program

import pandas as pd

# Initialize dataframe
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0]})

# Check if the values of df are in the range(1,6)
out = df.isin(range(1,6))

print('DataFrame\n-----------\n',df)
print('\nDataFrame.isin(range(1,6))\n-----------\n',out)
Run Code Copy

Output

DataFrame
-----------
    a  b
0  2  2
1  4  0

DataFrame.isin(range(1,6))
-----------
       a      b
0  True   True
1  True  False

Clearly, 2,4 are in the range(1,6), but 0 is not. Hence for the position of element 0, there is False returned by DataFrame.isin().

2. DataFrame.isin() with Series

In this example, we will apply DataFrame.isin() with a Series. The values of the DataFrame that match the values along with the index return True while other return False for the respective index of the DataFrame.

Python Program

import pandas as pd

# Initialize dataframe
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0]})

# Series
s = pd.Series([2, 0])

# Check if the values of df are in the range(1,6)
out = df.isin(s)

print('DataFrame\n-----------\n',df)
print('\nSeries\n-----------\n',s)
print('\nDataFrame.isin(series)\n-----------\n',out)
Run Code Copy

Output

D:\>python example.py
DataFrame
-----------
    a  b
0  2  2
1  4  0

Series
-----------
 0    2
1    0
dtype: int64

DataFrame.isin(series)
-----------
        a     b
0   True  True
1  False  True

Clearly, at the index 0,2,2 of DataFrame match the 2 of Series, but at index 1 the first element 4 did not match.

3. DataFrame.isin() with DataFrame

In this example, we will apply DataFrame.isin() with another DataFrame. The values of the DataFrame that match the values along with the index return True while other return False for the respective index of the DataFrame.

Python Program

import pandas as pd

# Initialize dataframe
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0]}, index=['i1', 'i2'])

# Series
df1 = pd.DataFrame({'a': [2, 4], 'b': [2, 3]}, index=['i1', 'i3'])

# Check if the values of df are in the range(1,6)
out = df.isin(df1)

print('DataFrame\n-----------\n',df)
print('\nDataFrame df1\n-----------\n',df1)
print('\nDataFrame.isin(df1)\n-----------\n',out)
Run Code Copy

Output

DataFrame
-----------
     a  b
i1  2  2
i2  4  0

DataFrame df1
-----------
     a  b
i1  2  2
i3  4  3

DataFrame.isin(df1)
-----------
         a      b
i1   True   True
i2  False  False

index i2 did not match with that of in df1. Hence, that row is returned all false values.

4. DataFrame.isin() with Dictionary

In this example, we will apply DataFrame.isin() with a dictionary. The keys of dictionary are considered as columns and the corresponding values are considered as the column values. isin(dict) returns True for those values where keys of dict matches with the column name and values of dictionary matches with the column values of DataFrame.

Python Program

import pandas as pd

# Initialize dataframe
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0]}, index=['i1', 'i2'])

# Dictionary
dict = {'a': [2, 4]}

# DataFrame.isin(dict)
out = df.isin(dict)

print('DataFrame\n-----------\n',df)
print('\nDictionary\n-----------\n',dict)
print('\nDataFrame.isin(dict)\n-----------\n',out)
Run Code Copy

Output

DataFrame
-----------
     a  b
i1  2  2
i2  4  0

Dictionary
-----------
 {'a': [2, 4]}

DataFrame.isin(dict)
-----------
        a      b
i1  True  False
i2  True  False

index i2 did not match with that of in df1. Hence, that row is returned all false values.

Summary

We have learnt how the usage of DataFrame.isin() with the argument: Iterable, Series, DataFrame or Dictionary.

Code copied to clipboard successfully 👍