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:
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
from_arrays
Now, let’s use the from_arrays method to create exactly the same MultiIndex:
b = pd.MultiIndex.from_arrays([['Asia', 'Asia', 'Australia', 'Australia'], [2019, 2020, 2019, 2020]])
b
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:
c = pd.MultiIndex.from_product([['Asia', 'Australia'], [2019, 2020]])
c
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:
d = pd.MultiIndex(levels=[['Asia', 'Australia'], [2019, 2020]],
codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
d
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:
# the data
data = [100, 200,
150, 250]
# Let's create a Series object with hierarchical indices.
s = pd.Series(data, index=a)
s
In the next part of the series we’ll be talking about named MultiIndex levels.