Skip to content
Home » pandas Part 19 – Indexing and Slicing Multiply Indexed Series

pandas Part 19 – Indexing and Slicing Multiply Indexed Series

Spread the love

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]:
species      region       
wild horses  Europe           1200000
             Asia             2500000
             North America     850000
boars        Europe           4900000
             Asia             5400000
             North America    3600000
wolves       Europe           2100000
             Asia             1900000
             North America     940000
dtype: int32

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]:
5400000
In [3]:
# If you use just one indexing level, you will get a partial Series.
populations_by_region['boars']
Out[3]:
region
Europe           4900000
Asia             5400000
North America    3600000
dtype: int32

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]:
species      region       
boars        Asia             5400000
             Europe           4900000
             North America    3600000
wild horses  Asia             2500000
             Europe           1200000
             North America     850000
wolves       Asia             1900000
             Europe           2100000
             North America     940000
dtype: int32
In [8]:
# Now we can slice the Series object.
populations_by_region['wild horses':'wolves']
Out[8]:
species      region       
wild horses  Asia             2500000
             Europe           1200000
             North America     850000
wolves       Asia             1900000
             Europe           2100000
             North America     940000
dtype: int32
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]:
species
boars          4900000
wild horses    1200000
wolves         2100000
dtype: int32
In [11]:
# Boolean masks also work.
populations_by_region[populations_by_region < 2000000]
Out[11]:
species      region       
wild horses  Europe           1200000
             North America     850000
wolves       Asia             1900000
             North America     940000
dtype: int32
In [15]:
# Finally, there's fancy indexing.
populations_by_region[['boars', 'wolves']]
Out[15]:
species  region       
boars    Asia             5400000
         Europe           4900000
         North America    3600000
wolves   Asia             1900000
         Europe           2100000
         North America     940000
dtype: int32

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply