Home > Software design >  pandas series || get index of string if present
pandas series || get index of string if present

Time:12-07

Please bear with me. I am new to pandas and using series within it.

import pandas as pd

s = pd.Series(["koala", "dog", "chameleon"])

'dog' in s.values - this allows test if it is present or otherwise.

s.isin(['dog']).any() - this works true. But there isn't any way to get index or use find.

How do I find index/location of "dog"?

Second, if I have duplicate entries (of dog, for e.g.):

s = pd.Series(["koala", "dog", "chameleon","dog"])

How would I be able to find first or last occurrence?

I am on python 3X (OS X, M1):
Python 3.9.8 (v3.9.8:bb3fdcfe95, Nov 5 2021, 16:40:46) [Clang 13.0.0 (clang-1300.0.29.3)] on darwin

CodePudding user response:

What I usually do is:

#....in clause like
i = s.index[s.str.contains('dog')]

That corresponds to an "in" clause. If you want the correct match you can go of "eq":

i2 = s.index[s.eq('dog')]

In this case, they produce the same index of course.

CodePudding user response:

You can use [].index to get the index of a value in a series.

s = pd.Series(["koala", "dog", "chameleon"])
s[s == 'dog'].index

Similarly to get the first and last occurrence using min() and max():

s = pd.Series(["koala", "dog", "chameleon","dog"])
d_first, d_last = s[s == 'dog'].index.min(), s[s == 'dog'].index.max()

CodePudding user response:

Change order of boolean mask for filter index and get last value in next with iter:

m = s.eq('dog')[::-1]
#if list
m = s.isin(['dog'])[::-1]

print (next(iter(m.index[m]), 'no match'))
3

If always match simplier is use:

print (m[m].index[-1])
3
  • Related