Performance benefits of stacked data
Finally, we will examine a reason for which we would want to stack data like this. This is because it can be shown to be more efficient than using lookup through a single level index and then a column lookup, or even compared to an .iloc
lookup, specifying the location of the row and column by location. The following demonstrates this:
In [53]: # stacked scalar access can be a lot faster than # column access # time the different methods import timeit t = timeit.Timer("stacked1[('one', 'a')]", "from __main__ import stacked1, df") r1 = timeit.timeit(lambda: stacked1.loc[('one', 'a')], number=10000) r2 = timeit.timeit(lambda: df.loc['one']['a'], number=10000) r3 = timeit.timeit(lambda: df.iloc[1, ...
Get Learning pandas now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.