Slicing a series with a hierarchical index

When it comes to slicing, series of the hierarchical index resemble NumPy multidimensional arrays. For example, if using the square bracket accessor, we simply separate levels of the hierarchical index with commas, and slice each level, imagining that they were separate indices for separate dimensions of some high-dimensional object. This holds for the loc method as well as for series, but not for DataFrames; we'll see what to do there later. All the usual tricks when slicing indices still work when using loc, but it's easier to get multiple results for a slicing operation.

So, let's see slicing a series of the MultiIndex in action. The first thing we're going to do is slice the first level, selecting ...

Get Hands-On Data Analysis with NumPy and 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.