Skip to content
Home » pandas Part 16 – Explicit Creation of Multi-Indexed Objects

pandas Part 16 – Explicit Creation of Multi-Indexed Objects

Spread the love

EXPLICIT CREATION OF MULTI-INDEXED OBJECTS

In the previous part of the Pandas series we were creating multi-indexed objects implicitly, so actually they were created behind the scenes. But sometimes you may want to create them explicitly. We can explicitly create MultiIndex objects by means of some methods in the MultiIndex class. In particular, these include: from_tuples (which we already used a couple times), from_arrays and from_product.

from_tuples

Let’s have a look at from_tuples one more time:

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

# Let's create a MultiIndex.
a = pd.MultiIndex.from_tuples([('Asia', 2019), ('Asia', 2020), ('Australia', 2019), ('Australia', 2020)])

a
Out[1]:
MultiIndex([(     'Asia', 2019),
            (     'Asia', 2020),
            ('Australia', 2019),
            ('Australia', 2020)],
           )

from_arrays

Now, let’s use the from_arrays method to create exactly the same MultiIndex:

In [2]:
b = pd.MultiIndex.from_arrays([['Asia', 'Asia', 'Australia', 'Australia'], [2019, 2020, 2019, 2020]])
b
Out[2]:
MultiIndex([(     'Asia', 2019),
            (     'Asia', 2020),
            ('Australia', 2019),
            ('Australia', 2020)],
           )

from_product

The two methods above have pretty self-explanatory names. The third method, from_product, is pretty simple too. It just creates a Cartesian product of single indices, so we get all possible combinations. Let’s re-create the MultiIndex that we just created, this time using the from_product method:

In [3]:
c = pd.MultiIndex.from_product([['Asia', 'Australia'], [2019, 2020]])
c
Out[3]:
MultiIndex([(     'Asia', 2019),
            (     'Asia', 2020),
            ('Australia', 2019),
            ('Australia', 2020)],
           )

LEVELS AND CODES

Besides the three methods, there are other ways to create MultiIndex objects. For example, you can explicitly pass the levels and codes. You already saw levels and codes in part 12 of the series. Here’s how we can use them to create a MultiIndex:

In [4]:
d = pd.MultiIndex(levels=[['Asia', 'Australia'], [2019, 2020]],
                  codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
d
Out[4]:
MultiIndex([(     'Asia', 2019),
            (     'Asia', 2020),
            ('Australia', 2019),
            ('Australia', 2020)],
           )

Now you can use the MultiIndex to create multi-indexed objects. All the MultiIndexes above (a, b, c, d) are identical, so we can use any of them. Here’s an example:

In [5]:
# the data
data = [100, 200,
        150, 250]

# Let's create a Series object with hierarchical indices.
s = pd.Series(data, index=a)
s
Out[5]:
Asia       2019    100
           2020    200
Australia  2019    150
           2020    250
dtype: int64

In the next part of the series we’ll be talking about named MultiIndex levels.

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