Open In App

Find location of an element in Pandas dataframe in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to find the position of an element in the dataframe using a user-defined function. Let’s first Create a simple dataframe with a dictionary of lists.

How to find an element in Pandas Dataframe?

In a Pandas DataFrame, you can find the position of a specific element or check for its existence using various methods. Here are a few common ways. Let’s create a dataframe first and see each method one by one.

Python3




import pandas as pd
 
# List of tuples
students = pd.DataFrame([('Ankit', 23, 'Delhi', 'A'),
            ('Swapnil', 22, 'Delhi', 'B'),
            ('Aman', 22, 'Dehradun', 'A'),
            ('Jiten', 22, 'Delhi', 'A'),
            ('Jeet', 21, 'Mumbai', 'B')
            ])
students


Output: 

    0    1    2    3
0 Ankit 23 Delhi A
1 Swapnil 22 Delhi B
2 Aman 22 Dehradun A
3 Jiten 22 Delhi A
4 Jeet 21 Mumbai B

Using isin() Method

The isin() method returns a DataFrame of the same shape as the input, with True at positions where the specified element exists.

Python3




# Check if 'Jiten' is in the DataFrame
result = students.isin(['Jiten'])
print(result)


Output:

       0      1      2      3
0 False False False False
1 False False False False
2 False False False False
3 True False False False
4 False False False False

Using any() Method

The any() method checks if the specified element exists in any column, returning a Boolean result.

indices[indices] to filter only the columns with True values. The loop then iterates over these columns, and for each column, it finds the row index where the value exists using students[col_index]. eq().idxmax(), where .idxmax(): Finds the index (row index) where the first occurrence of True appears.

Python3




indices = (students == 'Jiten').any() #using any to find index positions
indices


Output:

0     True
1 False
2 False
3 False
dtype: bool

Using NumPy’s where() Function

Returns indices where a specified condition is met in the DataFrame, useful for finding the position of an element.

Python3




indices = np.where(students == 'Jeet')
# Extracting row and column indices
row_indices, col_indices = indices[0], indices[1]
print(row_indices, col_indices)


Output:

[4] [0]

How to find location of multiple elements in the DataFrame

There are several ways to find the location of multiple elements in a DataFrame. Below are three common approaches.

Using isin()

The code snippet creates a list of target students you want to find. It then uses the isin() method on the 0th column to select rows. Finally, it retrieves the index of those rows using the .index attribute.

Python3




import pandas as pd
index_list = students[students.isin(['Ankit', 'Swapnil','Delhi'])]
print(index_list)


Output:

         0   1      2    3
0 Ankit NaN Delhi NaN
1 Swapnil NaN Delhi NaN
2 NaN NaN NaN NaN
3 NaN NaN Delhi NaN
4 NaN NaN NaN NaN

Frequently Asked Questions (FAQs)

1. How do you find an element in a DataFrame in Python?

Use .loc or .iloc to access specific rows or columns by label or index, then filter or compare as needed.

2.How do I access elements in Panda DataFrame?

Use brackets ([]) with row and column labels or indexes to access specific values.

3.How do I search for a value in Pandas DataFrame?

 Use .query with conditional expressions to filter rows based on element values.

4. How can I find out the index of an element from row and column in Python?

 Use .index.get_loc(element) to find the row index of a specific element, or .columns.get_loc(element) for the column index.
 



Last Updated : 21 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads