Python Pandas - Basics of MultiIndex



MultiIndex is also called Hierarchical Indexing, it is a powerful feature in pandas that allows you to work with higher-dimensional data in lower-dimensional structures like Series (1D) and DataFrame (2D). With MultiIndex, pandas objects have multiple levels of index labels. Using MultiIndex, you can represent and manipulate data with multiple levels of indexing, making it easier to handle complex data sets efficiently.

In this tutorial, we will learn about the basics of MultiIndex, including how to create MultiIndexed Series and DataFrames, perform basic indexing on MultiIndex axes, and align data using MultiIndex.

Creating MultiIndexed Pandas Objects

There are several ways to create a MultiIndex object in pandas, including from lists of arrays, tuples, products of iterables, or directly from a DataFrame.

Following are the list of helper methods to construct a new MultiIndex −

  • MultiIndex.from_arrays()

  • MultiIndex.from_product()

  • MultiIndex.from_tuples()

  • MultiIndex.from_frame()

Creating MultiIndex from Lists of Arrays

By using the pandas.MultiIndex.from_arrays() method we can create MultiIndex from list of arrays.

Example: Creating MultiIndexed Series from List of lists

The following example demonstrates the creation of MultiIndexed Series object using the pandas.MultiIndex.from_arrays() method.

Open Compiler
import pandas as pd import numpy as np # Create a 2D list list_2d = [["BMW", "BMW", "Lexus", "Lexus", "foo", "foo", "Audi", "Audi"], ["1", "2", "1", "2", "1", "2", "1", "2"]] # Create a MultiIndex object index = pd.MultiIndex.from_arrays(list_2d, names=["first", "second"]) # Creating a MultiIndexed Series s = pd.Series(np.random.randn(8), index=index) # Display the output Series print("Output MultiIndexed Series:\n",s)

Following is the output of the above code −

Output MultiIndexed Series:
First Second
BMW 1 -1.334159
2 -0.233286
Lexus 1 1.167558
2 -0.875364
foo 1 -0.338715
2 0.021517
Audi 1 -0.272688
2 0.588359
dtype: float64

Creating MultiIndex from Tuples

Pandas MultiIndex.from_tuples() method is used to convert list of tuples to MultiIndex.

Example: Creating MultiIndexed DataFrame from Tuples

This example demonstrates the creation of MultiIndexed DataFrame object using the pandas.MultiIndex.from_tuples() method.

Open Compiler
import pandas as pd import numpy as np # Create a 2D list list_2d = [["BMW", "BMW", "Lexus", "Lexus", "foo", "foo", "Audi", "Audi"], ["1", "2", "1", "2", "1", "2", "1", "2"]] # Create a MultiIndex object tuples = list(zip(*list_2d )) index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"]) # Creating a MultiIndexed DataFrame df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=["A", "B", "C", "D"]) # Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
A B C D
First Second
BMW 1 -0.347846 1.011760 -1.224244 -1.225259
2 0.237115 -1.316433 0.962960 -1.008623
Lexus 1 -1.806209 1.038973 0.246994 -1.596616
2 -0.325284 -0.264822 -0.735029 0.377645
foo 1 0.243560 -0.408718 0.717466 -1.446259
2 -0.817704 0.711299 2.567860 -1.054871
Audi 1 0.583519 -0.007577 0.828928 -0.826645
2 2.454626 1.558045 0.981507 -0.148554

Creating MultiIndex Using from_product()

The Pandas MultiIndex.from_product() method is uses the cartesian product of multiple iterables to create MultiIndex. It is useful when you want every possible combination of elements from two or more iterables.

Example: Creating MultiIndexed DataFrame Using from_product()

This example demonstrates how to create the MultiIndexed DataFrame using the pandas MultiIndex.from_product() method.

Open Compiler
import pandas as pd import numpy as np # Create a list of lits iterable = [[1, 2, 3], ['green', 'black']] # Create a MultiIndex object index = pd.MultiIndex.from_product(iterable, names=["number", "color"]) # Creating a MultiIndexed DataFrame df = pd.DataFrame(np.random.randn(6, 3), index=index, columns=["A", "B", "C"]) # Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
A B C
Number Color
1 green -1.174910 -0.861695 -0.026601
black -2.824289 0.674870 1.132675
2 green -0.285381 -0.104188 1.993371
black -0.926109 -0.579404 -1.119692
3 green -3.278989 -0.873407 -1.359360
black 0.735492 0.066735 -0.099568

Creating MultiIndex from DataFrame

The Pandas MultiIndex.from_frame() method is used to create a MultiIndex from a DataFrame.

Example: Creating MultiIndex from DataFrame

This example uses the pd.MultiIndex.from_frame() method to directly create a MultiIndex object from a DataFrame.

Open Compiler
import pandas as pd import numpy as np # Create a DataFrame df = pd.DataFrame([["BMW", 1], ["BMW", 2], ["Lexus", 1],["Lexus", 2]], columns=["first", "second"]) # Create a MultiIndex object index = pd.MultiIndex.from_frame(df) # Creating a MultiIndexed DataFrame df = pd.DataFrame(np.random.randn(4, 3), index=index, columns=["A", "B", "C"]) # Display the output Series print("Output MultiIndexed DataFrame:\n", df)

Following is the output of the above code −

Output MultiIndexed DataFrame:
A B C
First Second
BMW 1 -0.662779 -0.270775 0.129462
2 -0.251308 1.920896 0.756204
Lexus 1 -0.466133 0.590872 0.252439
2 -1.226775 -0.239043 -0.023214

Basic Indexing on Axis with MultiIndex

Indexing with MultiIndex used to slice and select data in more flexible ways compared to a regular index.

Example: Selecting Data by Index Level

Here is a basic example demonstrating the indexing MultiIndexed Series object using the .loc[] method.

Open Compiler
import pandas as pd import numpy as np # Creating MultiIndex from arrays arrays = [["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ["one", "two", "one", "two", "one", "two", "one", "two"]] # Creating a list of tuples from the arrays tuples = list(zip(*arrays)) # Creating a MultiIndex from tuples index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"]) # Creating a Series with MultiIndex s = pd.Series([2, 3, 1, 4, 6, 1, 7, 8], index=index) print("MultiIndexed Series:\n", s) # Indexing the MultiIndexed Series using .loc[] print("\nSelecting data at index ('bar', 'one') and column 'A':") print(s.loc[('bar', 'one')])

Following is the output of the above code −

MultiIndexed Series:
First Second
bar one 2
two 2
one 1 1
two 4
one 1 6
two 1
one 1 7
two 8
dtype: int64 Selecting data at index ('bar', 'one') and column 'A': 2
Advertisements