How to find last occurence index matching a certain value in a Pandas Series?

Viewed 10597

How do I find the last occurrence index for a certain value in a Pandas Series?

For example, let's say I have a Series that looks like follows:

s = pd.Series([False, False, True, True, False, False])

And I want to find the last index for a True value (i.e. index 3), how would you go about it?

4 Answers

Use last_valid_index:

s = pd.Series([False, False, True, True, False, False])
s.where(s).last_valid_index()

Output:

3

Using @user3483203 example

s = pd.Series(['dog', 'cat', 'fish', 'cat', 'dog', 'horse'], index=[*'abcdef'])
s.where(s=='cat').last_valid_index()

Output

'd'

Using nonzero

s.nonzero()[0][-1]
Out[66]: 3

You can use np.argmax on your reversed Series if you are looking in a boolean array:

>>> len(s) - np.argmax(s[::-1].values) - 1
3

If you are looking for another value, just convert it to a boolean array using ==

Here's an example looking for the last occurence of dog:

>>> s = pd.Series(['dog', 'cat', 'fish', 'cat', 'dog', 'horse'])
>>> len(s) - np.argmax(s[::-1].values=='dog') - 1
4

However, this will give you a numeric index. If your series has a custom index it will not return that.

You can use a generator expression with next and enumerate:

s = pd.Series([False, False, True, True, False, False])

res = len(s) - next(idx for idx, val in enumerate(s[::-1], 1) if val)  # 3

This will be more efficient for large series with a True value towards the end.

Related