In this article, we will discuss how to find index positions of a given value in the dataframe i.e. row & column numbers.

Let’s create a dataframe i.e.

Content of the dataframe is,

Now we want to find the exact locations in the dataframe where value ’81’ exists. Like,

It shows that value 81 exists at 2 places in the above dataframe i.e.

  1. At row index 4 & column “Age”
  2. At row index 2 & column “Marks”

Now let’s see how to get this kind of results.

Find all indexes of an item in pandas dataframe

We have created a function that accepts a dataframe object and a value as argument. It returns a list of index positions ( i.e. row,column) of all occurrences of the given value in the dataframe i.e.

Contents of our dataframe empDfObj are,

Let’s use above created function getIndexes() to find the all index positions of 81 in the dataframe i.e.

Output:

We got our result i.e. exact row indexes & column names of all locations where 81 exists in the dataframe. But what just happened inside the getIndexes() function?

How did it work?

Let’s understand step by step that what happened in the function getIndexes() ,

Step 1: Get bool dataframe with True at positions where value is 81 in the dataframe using pandas.DataFrame.isin()

Dataframe provides a function isin(), which accepts values and returns a bool dataframe. This bool dataframe is of the same size as the original dataframe, it contains True at places where given values exist in the dataframe, at other places it contains False.

Let’s get a bool dataframe with True at positions where value is 81 i.e.

Output

It is of the same size as empDfObj. As 81 exists at 2 places inside the dataframe, so this bool dataframe contains True at only those two places. At all other places it contains False.

Step 2 : Get list of columns that contains the value

Now we are going to fetch the names of columns that contain value 81. We can achieve this by fetching names of columns in bool dataframe which contains any True i.e.

Output

Step 3 : Iterate over selected columns and fetch the indexes of the rows which contains the value

Now in the bool dataframe iterate over each of the selected columns and for each column find rows which contains True. Now these combinations of column names and row indexes where True exists are the index positions of 81 in the dataframe i.e.

Output

This is how getIndexes() founds the exact index positions of the given value & store each position as (row, column) tuple. In the end it returns a list of tuples representing its index positions in the dataframe.

Find index positions of multiple elements in the DataFrame

Suppose we have multiple elements i.e.

Now we want to find index positions of all these elements in our dataframe empDfObj, like this,

Let’s use the getIndexes() and dictionary comprehension to find the indexes of all occurrences of multiple elements in the dataframe empDfObj,

Output

dictOfPos is a dictionary of elements and their index positions in the dataframe. As ‘abc‘ doesn’t exist in the dataframe, therefore, its list is empty in dictionary dictOfPos.

The complete example is as follows,

Output:

Click Here to Subscribe for more Articles / Tutorials like this.