Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (17.6k points)

I know this is a very basic question but for some reason I can't find an answer. How can I get the index of certain element of a Series in python pandas? (first occurrence would suffice)

I.e., I'd like something like:

import pandas as pd

myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4])

print myseries.find(7) # should output 3

Certainly, it is possible to define such a method with a loop:

def find(s, el):

    for i in s.index:

        if s[i] == el: 

            return i

    return None

print find(myseries, 7)

but I assume there should be a better way. Is there?

1 Answer

0 votes
by (41.4k points)

To find element index in pandas series, use this:

 myseries[myseries == 8]

3    8

dtype: int64

myseries[myseries == 8].index[0]


Related questions

0 votes
3 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers


108k users

Browse Categories