Reputation: 29456
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?
Upvotes: 17
Views: 16224
Reputation: 153460
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'
Upvotes: 22
Reputation: 164673
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.
Upvotes: 2
Reputation: 51155
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.
Upvotes: 2