Pandas find index of value in Python [4 Methods]

Are you interested in finding the index of the value in a dataframe? In this Pandas article, I will explain, how Pandas find index of value in Python using some different methods with examples.

To find the index of a value in pandas in Python, several methods can be used. The .index property retrieves the index where a condition is met, while index.values returns these indices as a NumPy array. The tolist() function converts the indices into a Python list for easy iteration, and the np.where() function efficiently finds indices for matching conditions, particularly useful for large datasets.

Pandas find index of value in Python

There are four different kinds of methods that Pandas find index of value in Python:

  1. Using the .index Property
  2. Using index.values
  3. Using tolist() Function
  4. Using np.where() Function

Let’s see them one by one using some demonstrative examples:

1. Get index of row Pandas using the .index property

The .index property in pandas is utilized to retrieve the indices of rows in a DataFrame or Series that match a specified condition. It’s a straightforward Python method where we can apply a condition to the DataFrame or Series and use .index to get the indices where this condition is True.

This way we can use the .index property, for Pandas find index of value in Python:

import pandas as pd

df = pd.DataFrame({'State': ['New York', 'California', 'Texas'],
                   'Population': [19450000, 39560000, 29000000]})
index_of_california = df[df['State'] == 'California'].index
print(index_of_california)

Output:

Index([1], dtype='int64')

Following the execution of the code in the Pycharm editor, a screenshot illustrating the result is presented below.

Pandas find index of value in Python

2. Pandas get index values using the index.values

The index.values convert the indices obtained from a condition into a NumPy array in Python. This is particularly useful when we need the indices in a format compatible with NumPy operations or for further array manipulations.

Here is the code that we can use index.values, for Pandas find index of value in Python:

import pandas as pd

df = pd.DataFrame({'City': ['Los Angeles', 'Chicago', 'Houston'],
                   'Average Temperature (F)': [66, 50, 70]})
index_of_chicago = df[df['City'] == 'Chicago'].index.values
print(index_of_chicago)

Output:

[1]

Upon completing the code execution in Pycharm, the resulting output is visible in the screenshot provided below.

get index of row pandas in Python

3. Pandas get index of value using the tolist() Function

The tolist() function is used to convert the indices, obtained from a conditional check in pandas, into a standard Python list. This method is handy when we need the indices in a list format, perhaps for iterating over them or for other list-based operations.

This is the code that tells how to use tolist() function for Pandas find index of value in Python:

import pandas as pd

df = pd.DataFrame({'University': ['Harvard University', 'Stanford University', 'MIT'],
                   'Founded': [1636, 1885, 1861]})
index_of_harvard = df[df['University'] == 'Harvard University'].index.tolist()
print(index_of_harvard)

Output:

[0]

The screenshot below illustrates the output obtained after implementing and running the code in the Pycharm editor.

pandas get index of row in Python

4. Pandas dataframe find index of value using np.where() function

The np.where() function is a versatile tool for finding indices of elements that meet a certain condition in a pandas DataFrame or Series in Python. It returns indices as a NumPy array and is especially efficient for large datasets, offering a more performant alternative to native pandas methods.

Here is the code to use np.where() function, Pandas find index of value in Python:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Airline': ['American Airlines', 'Delta Air Lines', 'United Airlines'],
                   'Destinations': [350, 325, 342]})
index_of_delta = np.where(df['Airline'] == 'Delta Air Lines')[0]
print(index_of_delta)

Output:

[1]

The following screenshot displays the result of the code implemented in the Pycharm editor.

get index of dataframe in Pandas Python

Conclusion

Here, I have illustrated various methods to find the index of a value in pandas in Python. These include using the .index property for straightforward index retrieval, index.values to get a NumPy array of indices, the tolist() function for converting indices into a list, and the np.where() function for identifying indices in complex or large datasets.

Each approach offers unique advantages depending on the data analysis requirements.

You may also like to read: