INDEXING AND SLICING MULTIPLY INDEXED SERIES
Indexing and slicing multiply indexed Series is intuitive. Let’s use one of the examples we saw before:
In [1]:
import numpy as np
import pandas as pd
p = pd.MultiIndex.from_product([['wild horses', 'boars', 'wolves'], ['Europe', 'Asia', 'North America']],
names=['species', 'region'])
populations = np.array([1200, 2500, 850,
4900, 5400, 3600,
2100, 1900, 940])
populations *= 1000
populations_by_region = pd.Series(populations, index=p)
populations_by_region
Out[1]:
Here we have something we can demonstrate indexing and slicing on. And now let’s have a look at some examples:
In [2]:
# To access a single element we use multiple indices.
populations_by_region['boars', 'Asia']
Out[2]:
In [3]:
# If you use just one indexing level, you will get a partial Series.
populations_by_region['boars']
Out[3]:
For slicing to work, the indices must be sorted. Otherwise we’ll get an error.
We can sort them using the sort_index method that we will discuss in one of the following parts of the pandas series. So, let’s sort the indices and assign the result back to the original Series object:
In [7]:
populations_by_region = populations_by_region.sort_index()
populations_by_region
Out[7]:
In [8]:
# Now we can slice the Series object.
populations_by_region['wild horses':'wolves']
Out[8]:
In [9]:
# You can also use partial indexing on lower levels. Then we pass an empty slice for the higher level.
populations_by_region[:, 'Europe']
Out[9]:
In [11]:
# Boolean masks also work.
populations_by_region[populations_by_region < 2000000]
Out[11]:
In [15]:
# Finally, there's fancy indexing.
populations_by_region[['boars', 'wolves']]
Out[15]: