December 2018
Beginner to intermediate
682 pages
18h 1m
English
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().values1.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].index5.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 ...