Last updated: Apr 12, 2024
Reading timeยท5 min

To find an element's index in a Series in Pandas:
Series containing the element's index in
the first position.index attribute to get the first element in the Series.import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # 2 5 # dtype: int64 print(series[series == 5]) print('-' * 50) index_of_5 = series[series == 5].index[0] print(index_of_5) # ๐๏ธ 2

We first used bracket notation to get a new Series object that contains the
element's position at index 0.
# 2 5 # dtype: int64 print(series[series == 5])
The next step is to use the Series.index property to get the first element.
index_of_5 = series[series == 5].index[0] print(index_of_5) # ๐๏ธ 2
This approach also works when there are multiple occurrences of the same value
in the Series.
import pandas as pd series = pd.Series([5, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # 0 5 # 2 5 # dtype: int64 print(series[series == 5]) print('-' * 50) print(series[series == 5].index[0]) # ๐๏ธ 0 print(series[series == 5].index[1]) # ๐๏ธ 2
If you need to convert the Series of indices to a list, use the
list class.
import pandas as pd series = pd.Series([5, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # ๐๏ธ [0, 2] print(list(series[series == 5].index))
try/exceptWhen using this approach, you have to make sure the specified element exists in
the Series, otherwise, you'd get an IndexError.
You can use a try/except statement if you need to handle the potential error.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # Series([], dtype: int64) print(series[series == 100]) print('-' * 50) try: index_of_100 = series[series == 100].index[0] print(index_of_100) except IndexError: # ๐๏ธ this runs print('The given element does NOT exist in the Series')

The Series in the example doesn't contain a 100 value, so the expression
returns a new, empty Series object.
Trying to access the empty Series object at index 0 causes an error, so we
wrapped the code in a try/except block.
If the given element doesn't exist, the except block runs.
Index() classYou can also find an element's index in a Series by converting the Series to
an Index
object and using the
get_loc()
method.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_5 = pd.Index(series).get_loc(5) print(index_of_5) # ๐๏ธ 2 print(series[index_of_5]) # ๐๏ธ 5

We used the Index() constructor to convert the Series to an Index object.
Index objects are immutable sequences that are used for indexing and alignment.
Index objects have a get_loc method that returns the index of the supplied value.
If the given value doesn't exist in the Series, you would get a
KeyError exception.
You can use a try/except block to handle the potential error.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) try: index_of_100 = pd.Index(series).get_loc(100) print(index_of_100) except KeyError: # ๐๏ธ This runs print('The given value does NOT exist in the Series')

If there are multiple occurrences of the same value in the Series, the
get_loc method returns a boolean array.
import pandas as pd series = pd.Series([5, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_5 = pd.Index(series).get_loc(5) # ๐๏ธ [ True False True False False False] print(index_of_5) print(series[index_of_5].index[0]) # ๐๏ธ 0 print(series[index_of_5].index[1]) # ๐๏ธ 2

The array contains True values for the matching elements.
where()You can also use the
Series.where()
method to find an element's index in a Series.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_5 = series.where(series == 5).first_valid_index() print(index_of_5) # ๐๏ธ 2

The Series.where method replaces the values where the given condition is
False.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # 0 NaN # 1 NaN # 2 5.0 # 3 NaN # 4 NaN # 5 NaN # dtype: float64 print(series.where(series == 5))
The last step is to use the first_valid_index method to get the index of the first non-NA value.
If the given value is not in the Series, the first_valid_index() method will
return None.
You can use an if statement if you need to
check if the variable is or is not None.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_100 = series.where(series == 100).first_valid_index() print(index_of_100) # ๐๏ธ None if index_of_100 is not None: print(index_of_100) else: # ๐๏ธ this runs print('The supplied value is NOT in the Series')
The supplied value doesn't exist in the Series, so None is returned.
argmax()You can also use the
Series.argmax()
method to find an element's index in a Series.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_5 = (series == 5).argmax() print(index_of_5) # ๐๏ธ 2

The Series.argmax() method returns the index of the largest value in the
Series.
We first used a condition to get a boolean Series that contains a True value
for the item that meets the condition.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) # 0 False # 1 False # 2 True # 3 False # 4 False # 5 False # dtype: bool print(series == 5)
True values get converted to 1 and False values get converted to 0, so the
argmax() method returns the index of the True value.
list()You can also convert the Series to a list and use the list.index() method
to find an element's index in a Series.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) index_of_5 = list(series).index(5) print(index_of_5) # ๐๏ธ 2

We first used the list() class to convert the Series to a list.
This enables us to use the list.index() method.
The list.index() method returns the first index of the supplied value.
If the value doesn't exist in the list, a ValueError is raised.
You can use a try/except block if you need to handle the potential error.
import pandas as pd series = pd.Series([1, 3, 5, 7, 9, 11], index=[0, 1, 2, 3, 4, 5]) try: index_of_100 = list(series).index(100) print(index_of_100) except ValueError: # ๐๏ธ this runs print('The value does NOT exist in the Series')

You can learn more about the related topics by checking out the following tutorials: