In this article we will dicuss different ways to check if a given value exists in the dataframe or not.

First of all, we need to import the pandas module i.e.

Let’s create a dataframe,

Contents of the dataframe :

Now how to check the existence of single or multiple values in dataframe ?
Let’s understand by examples,

Check if a single element exists in DataFrame using in & not in operators

Dataframe class provides a member variable i.e DataFrame.values . It returns a numpy representation of all the values in dataframe.
We can use the in & not in operators on these values to check if a given element exists or not. For example,

Use in operator to check if an element exists in dataframe

Check if 81 exists in the dataframe empDfObj i.e.

Output:

Use not in operator to check if an element doesn’t exists in dataframe

Check if ‘Hello’ does not exists in dataframe empDfobj i.e.

Output:

Check if multiple elements exists in DataFrame or not using in operator

Suppose we want to check that out of 3 given elements, how many exists in the dataframe ?

To do that we have created a function that accepts a elements to be checked in a list. It then iterates over that list and for each element it checks if that element exists in the dataframe values or not. In the end it returns a dictionary representing the existence of given element in dataframe,

Now let’s use this function to check if 81, ‘hello’ & 167 exists in the dataframe,

Output

Our function returned the dictionary which shows that 81 & 167 exists in the dataframe but ‘hello’ doesn’t exists in the dataframe.

Now instead of creating a separate function for this small task, we can use Dictionary Comprehension too i.e.

Output:

It works in the same fashion and returns a similar dictionary.

Check if elements exists in DataFrame using isin() function

We can also check the existence of single or multiple elements in dataframe using DataFrame.isin() function.

Arguments:

  • values:
    • iterable, Series, DataFrame or dict to be checked for existence.

It returns a bool dataframe representing that each value in the original dataframe matches with anyone of the given values.

Now let’s use isin() to check the existence of elements in dataframe,

Check if a single element exist in Dataframe using isin()

Contents of the dataframe empDfObj are,

Now let’s pass the [81] in isin() i.e.

It returns a bool dataframe boolDf , whose contents are,

The size of returned bool dataframe will be same as original dataframe but it contains True where 81 exists in the Dataframe.

Now if call any() on this bool array it will return a series showing if a column contains True or not i.e.

It returns a series object,

It shows the columns Age & Marks contains the True.

Now again call any() on this series object i.e.

It returns a bool i.e.

It returns a bool value representing that Series contains a True.

So basically,

Returns a True as all the values in list exists in the Dataframe. For example,

Output:

Check if any of the given values exists in the Dataframe

Using above logic we can also check if a Dataframe contains any of the given values. For example, check if dataframe empDfObj contains either 81, ‘hello’ or 167 i.e.

Output

It shows that yes our dataframe contains any of the given values.

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,