Skip to content
Home » pandas Part 14 – Using Pandas MultiIndex for Multidimensional Data

pandas Part 14 – Using Pandas MultiIndex for Multidimensional Data

Spread the love

USING PANDAS MULTIINDEX FOR MULTIDIMENSIONAL DATA

We can use the MultiIndex for multidimensional data. Here’s our example from the previous part of the series:

In [2]:
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)

# Here's the multi-index.
m_index = pd.MultiIndex.from_tuples(index)

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

Let’s convert the Series to a DataFrame. Let’s also make clear what sort of data the column contains:

In [3]:
s_df = pd.DataFrame({'sales': s})
s_df
Out[3]:
sales
Company A 2018 125
2019 211
Company B 2018 390
2019 455
Company C 2018 475
2019 655

Now, what if we wanted to add one more dimension to the DataFrame? For example annual purchases? We can do it like so:

In [4]:
# Let's define the purchases as p:
p = [44, 72,
    81, 88,
    93, 87]

# Let's add the purchases column.
s_df2 = pd.DataFrame({'sales': s,
                     'purchases': p})

s_df2
Out[4]:
sales purchases
Company A 2018 125 44
2019 211 72
Company B 2018 390 81
2019 455 88
Company C 2018 475 93
2019 655 87

You can also use all the ufuncs with MultiIndex. Let’s calculate the difference between the sales and purchases for example:

In [5]:
b = s_df2['sales'] - s_df2['purchases']
b
Out[5]:
Company A  2018     81
           2019    139
Company B  2018    309
           2019    367
Company C  2018    382
           2019    568
dtype: int64

This will even look better if we unstack it:

In [6]:
b.unstack()
Out[6]:
2018 2019
Company A 81 139
Company B 309 367
Company C 382 568

It works the same for higher-dimensional data.

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