Skip to content
Home » pandas Part 12 – Pandas MultiIndex

pandas Part 12 – Pandas MultiIndex

Spread the love

PANDAS MULTI-INDEX

The two data structures that you know, Series and DataFrame, are basically used for one- and two-dimensional data respectively. But you can also use them with higher-dimensional data. It consists of creating several index levels within a single index. We call it multi-indexing.

Let’s jump right into an example. Suppose we have data concerning the sales by several companies in a city. We have the data for each company for two yearly time periods. We’ll start by creating a list of tuples where each tuple contains the name of the company and one yearly period. Then we’ll create a list with the sales. To keep things simple, the sales will be in thousands, so if there is 450, it means $450,000. Then we’ll make a Series object from the list using the tuples as indices. Have a look:

In [3]:
import numpy as np
import pandas as pd

# the tuples used as indices
index = [('Company A', 2018), ('Company A', 2019),
        ('Company B', 2018), ('Company B', 2019),
        ('Company C', 2018), ('Company C', 2019)]

# the list of sales
sales = [125, 211,
        390, 455,
        475, 655]

# Let's create a Series object.
s = pd.Series(sales, index=index)

# Let's print out the Series object.
s
Out[3]:
(Company A, 2018)    125
(Company A, 2019)    211
(Company B, 2018)    390
(Company B, 2019)    455
(Company C, 2018)    475
(Company C, 2019)    655
dtype: int64

We’re only halfway there. Although you could use tuple indices like the ones above, it’s not very convenient or efficient. So, let’s move on and create a multi-index. We already havethe tuples, so we can use the from_tuples method:

In [4]:
# Here's the multi-index.
m_index = pd.MultiIndex.from_tuples(index)

# It looks like so:
m_index
Out[4]:
MultiIndex([('Company A', 2018),
            ('Company A', 2019),
            ('Company B', 2018),
            ('Company B', 2019),
            ('Company C', 2018),
            ('Company C', 2019)],
           )

A multi-index contains several levels of indexing. In our case there are two: the company names and the time periods. It also contains several codes, which are used to access to elements contained in the levels. Let’s check them out:

In [5]:
# Here are the levels.
m_index.levels
Out[5]:
FrozenList([['Company A', 'Company B', 'Company C'], [2018, 2019]])
In [7]:
# Here are the codes.
m_index.codes
Out[7]:
FrozenList([[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])

If you have a look at the codes, you’ll notice that if you take the elements from both FrozenLists in pairs (the ones at the same positions in the lists), you’ll see how the pieces of data in the levels are related to one another. So, for example the first pair is (0, 0), which means the first elements in both levels lists: (‘Company A’, 2018), the next pair is (0, 1), which corresponds to (‘Company A’, 2019). The third pair is (1, 0), which corresponds to (‘Company B’, 2018), and so on.

If you want to see the data in a hierarchical way, you have to reindex your data:

In [9]:
# Let's reindex the Series object.
s = s.reindex(m_index)
s
Out[9]:
Company A  2018    125
           2019    211
Company B  2018    390
           2019    455
Company C  2018    475
           2019    655
dtype: int64

Now, the missing data in some lines just means the same data is meant as in the line above. In the example above the two first columns belong to the index and the last column to the data.

You can easily access the data using the multi-index. Here an example:

In [10]:
# the 2019 period for all companies
s[:, 2019]
Out[10]:
Company A    211
Company B    455
Company C    655
dtype: int64

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.

Here’s the video version of this article:


Spread the love

Leave a Reply