The Python Oracle

Find index of last true value in pandas Series or DataFrame

This video explains
Find index of last true value in pandas Series or DataFrame

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island

--

Chapters
00:00 Question
00:33 Accepted answer (Score 22)
01:44 Answer 2 (Score 11)
02:12 Answer 3 (Score 4)
02:34 Thank you

--

Full question
https://stackoverflow.com/questions/3438...

Accepted answer links:
[idxmax]: http://pandas.pydata.org/pandas-docs/sta...
[argmax]: http://pandas.pydata.org/pandas-docs/sta...
[answer]: https://stackoverflow.com/a/34385194/290...
[thanks.]: https://stackoverflow.com/questions/3438...

Answer 2 links:
[last_valid_index]: http://pandas.pydata.org/pandas-docs/sta...

Answer 3 links:
[argmax]: http://pandas.pydata.org/pandas-docs/sta...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 24


You can use idxmax what is the same as argmax of Andy Hayden answer:

print s[::-1].idxmax()

Comparing:

These timings are going to be very dependent on the size of s as well as the number (and position) of Trues - thanks.

In [2]: %timeit s.index[s][-1]
The slowest run took 6.92 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 35 µs per loop

In [3]: %timeit s[::-1].argmax()
The slowest run took 6.67 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 126 µs per loop

In [4]: %timeit s[::-1].idxmax()
The slowest run took 6.55 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 127 µs per loop

In [5]: %timeit s[s==True].last_valid_index()
The slowest run took 8.10 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 261 µs per loop

In [6]: %timeit (s[s==True].index.tolist()[-1])
The slowest run took 6.11 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 239 µs per loop

In [7]: %timeit (s[s==True].index[-1])
The slowest run took 5.75 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 227 µs per loop

EDIT:

Next solution:

print s[s==True].index[-1]

EDIT1: Solution

(s[s==True].index.tolist()[-1])

was in deleted answer.




ANSWER 2

Score 11


Use last_valid_index:

In [9]:
s.tail(10)

Out[9]:
h    False
w     True
h    False
r     True
q    False
b    False
p    False
e    False
q    False
d    False
dtype: bool

In [8]:
s[s==True].last_valid_index()

Out[8]:
'r'



ANSWER 3

Score 4


argmax gets the first True. Use argmax on the reversed Series:

In [11]: s[::-1].argmax()
Out[11]: 'e'

Here:

In [12]: s.tail()
Out[12]:
n     True
e     True
k    False
d    False
l    False
dtype: bool