Multi-index in Python Pandas

Multi-index allows you to select more than one row and column in your index.

It is a multi-level or hierarchical object for Pandas object.

We can use various methods of multi-index such as MultiIndex.from_arrays(), MultiIndex.from_tuples(), MultiIndex.from_product(), MultiIndex.from_frame, etc., which helps us to create multiple indexes from arrays, tuples, DataFrame, etc.

Syntax

pandas.MultiIndex(levels=None, codes=None, sortorder=None, names=None, dtype=None, copy=False, name=None, verify_integrity=True)

Parameters

  • levels: It is a sequence of arrays that shows the unique labels for each level.
  • codes: It is also a sequence of arrays where integers at each level help us to designate the labels in that location.
  • sortorder: optional int. It helps us to sort the levels lexicographically.
  • dtype:data-type(size of the data which can be of 32 bits or 64 bits)
  • copy: It is a boolean type parameter with a default value of False. It helps us to copy the metadata.
  • verify_integrity: It is a boolean type parameter with a default value of True. It checks the integrity of the levels and codes i.t if they are valid.

Let us see some examples to understand the concept better.

Example 1: Creating multi-index from arrays

After importing all the important Python libraries, we are creating an array of names along with arrays of marks and age respectively.

Now with the help of MultiIndex.from_arrays, we are combining all three arrays such that elements from all three arrays form multiple indexes together. After that, we show the above result.

Python3
# importing pandas library from 
# python
import pandas as pd

# Creating an array of names
arrays = ['Sohom','Suresh','kumkum','subrata']

# Creating an array of ages
age= [10, 11, 12, 13]

# Creating an array of marks
marks=[90,92,23,64]

# Using MultiIndex.from_arrays, we are
# combining the arrays together along 
# with their names and creating multi-index 
# with each element from the 3 arrays into
# different rows
multi_index = pd.MultiIndex.from_arrays([arrays,age,marks], names=('names', 'age','marks'))
# Showing the above data
print(multi_index)

Output: 

Example 2: Creating multi-index from DataFrame using Pandas. 

In this example, we are doing the same thing as the previous example. We created a DataFrame using pd.DataFrame and after that, we created multi-index from that DataFrame using multi-index.from_frame() along with the names 

Python3
# importing pandas library from
# python
import pandas as pd

# Creating data
Information = {'name': ["Saikat", "Shrestha", "Sandi", "Abinash"],
               
               'Jobs': ["Software Developer", "System Engineer",
                        "Footballer", "Singer"],
               
               'Annual Salary(L.P.A)': [12.4, 5.6, 9.3, 10]}

# Dataframing the whole data
df = pd.DataFrame(dict)

# Showing the above data
print(df)

Output:

Now using MultiIndex.from_frame(), we are creating multiple indexes with this DataFrame.

Python3
# creating multiple indexes from 
# the dataframe
pd.MultiIndex.from_frame(df)

Output:

Example 3:Using DataFrame.set_index([col1,col2,..])

After importing the Pandas library, we created data and then converted it into tabular form with the help of pandas.DataFrame.

After that using Dataframe.set_index we are setting some columns as the index columns(Multi-Index).

The drop parameter is kept as false which will not drop the columns mentioned as index columns and thereafter append parameter is used for appending passed columns to the already existing index columns. 

Python3
# importing the pandas library
import pandas as pd

# making data for dataframing
data = {
    'series': ['Peaky blinders', 'Sherlock', 'The crown',
               'Queens Gambit', 'Friends'],
    
    'Ratings': [4.5, 5, 3.9, 4.2, 5],
    
    'Date': [2013, 2010, 2016, 2020, 1994]
}

# Dataframing the whole data created
df = pd.DataFrame(data)

# setting first and the second name
# as index column
df.set_index(["series", "Ratings"], inplace=True,
             append=True, drop=False)
# display the dataframe
print(df)

Output:

Now, we are printing the index of DataFrame in the form of a multi-index.

Python3
print(df.index)

Output: 

Pandas – Multi-index and Groupby Tutorial

Multi-index and Groupby are very important concepts of data manipulation. Multi-index allows you to represent data with multi-levels of indexing, creating a hierarchy in rows and columns.

Groupby lets you create groups of similar data and apply aggregate functions (e.g., mean, sum, count, standard deviation) to each group, condensing large datasets into meaningful summaries.

Using both these tools together allows you to analyze data from a different aspect.

In this article, we will discuss Multi-index for Pandas Dataframe and Groupby operations.

Similar Reads

Multi-index in Python Pandas

Multi-index allows you to select more than one row and column in your index....

GroupBy in Python Pandas

A groupby operation in Pandas helps us to split the object by applying a function and there-after combine the results....

Conclusion

We have covered the concept of Multi index and groupby in Pandas Python in this tutorial. Both these concepts are very crucial in data manipulation while doing data analysis....

Contact Us