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 (ie index 3), how would you go about it?
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.
The best solution I found to this problem is the following, assuming the pandas Series is stored in a variable s
~s.duplicated(keep="last")
This returns a pandas series indicating whether the row contains the last appearance of each value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.