Pandas DataFrame isin() Method
In this tutorial, we will discuss and learn the Python pandas DataFrame.isin()
method. This method checks whether each element in the DataFrame is contained in specified values. When this method applied on the DataFrame, it returns the DataFrame of booleans. If the DataFrame consists of True, the element is present in the specified value otherwise it shows False.
The below is the syntax of the DataFrame.isin()
method.
Syntax
DataFrame.isin(values)
Parameters
values: It can be iterable, Series, DataFrame, or dict. If values are a Series, that’s the index. If values are a dict, the keys must be the column names, which must match. If values are a DataFrame, then both the index and column labels must match.
Example 1: Checking values using the DataFrame.isin()
Method
Here, we are checking the DataFrame with the list
using the DataFrame.isin()
method. It returns the DataFrame of boolean values that is True if every value in the DataFrame present in the list
otherwise False. See the below example.
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0], 'c':[3, 5]})
print("-----The DataFrame is----")
print(df)
print("----------------------")
df_1=df.isin([2,3])
print(df_1)
Once we run the program we will get the following output.
-----The DataFrame is----
a b c
0 2 2 3
1 4 0 5
----------------------
a b c
0 True True True
1 False False False
Example 2: Checking values using the DataFrame.isin()
Method
Here, we are checking the DataFrame with the Series
using the DataFrame.isin()
method. It returns the DataFrame of boolean values that is True if every value in the DataFrame present in the Series
otherwise False. See the below example.
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({'a': [2, 4], 'b': [2, 0], 'c':[3, 5]})
print("-----The DataFrame is----")
print(df)
print("----------------------")
series=pd.Series([2,0,3])
df_1=df.isin(series)
print(df_1)
Once we run the program we will get the following output.
-----The DataFrame is----
a b c
0 2 2 3
1 4 0 5
----------------------
a b c
0 True True False
1 False True False
Example 3: Checking values using the DataFrame.isin()
Method
Here, we are checking the DataFrame with the other DataFrame
using the DataFrame.isin()
method. It returns the DataFrame of boolean values that is True if every value in the DataFrame present in the other DataFrame
otherwise False. See the below example.
import pandas as pd
#creating the DataFrame
df_1= pd.DataFrame({'a': [2, 4], 'b': [2, 0], 'c':[3, 5]})
print("-----The DataFrame is----")
print(df_1)
print("----------------------")
df_2= pd.DataFrame({'a': [0, 4], 'b': [1, 0], 'c':[3, 2]})
print(df_2)
print("----------------------")
print(df_1.isin(df_2))
Once we run the program we will get the following output.
-----The DataFrame is----
a b c
0 2 2 3
1 4 0 5
----------------------
a b c
0 0 1 3
1 4 0 2
----------------------
a b c
0 False False True
1 True True False
Example 4: Checking values using the DataFrame.isin()
Method
Here, we are checking the DataFrame with the dictionary
using the DataFrame.isin()
method. It returns the DataFrame of boolean values that is True if every value in the DataFrame present in the dictionary
otherwise False. See the below example.
import pandas as pd
#creating the DataFrame
df_1= pd.DataFrame({'a': [2, 4], 'b': [2, 0], 'c':[3, 5]})
print("-----The DataFrame is----")
print(df_1)
print("----------------------")
dict = {'a': [2, 1]}
print(dict)
print("----------------------")
print(df_1.isin(dict))
Once we run the program we will get the following output.
-----The DataFrame is----
a b c
0 2 2 3
1 4 0 5
----------------------
{'a': [2, 1]}
----------------------
a b c
0 True False False
1 False False False
Conclusion:
In this tutorial, we learned the Python pandas DataFrame.isin()
method. We learned the syntax and applying this method on the DataFrame with examples.