There's more...

It is possible to complete this recipe in one long line of code chaining the indexing operator with an anonymous function. This little trick removes the need for step 10. We can time the difference between the direct idxmax method and our manual effort in this recipe:

>>> %timeit college_n.idxmax().values
1.12 ms ± 28.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

>>> %timeit college_n.eq(college_n.max())
.cumsum()
.cumsum()
.eq(1)
.any(axis='columns')
[lambda x: x].index
5.35 ms ± 55.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Our effort is, unfortunately, five times as slow as the built-in idxmax pandas method but regardless of its performance regression, many creative and practical solutions use the accumulation methods like cumsum with boolean Series to find streaks or specific patterns along an axis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.209.201