Python - Using .loc with a MultiIndex in pandas

In this article, we are going to learn how to use loc[] property in multiindex dataframes?
Submitted by Pranit Sharma, on August 22, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

MultiIndex / Advanced Indexing

Multilevel indexing is a type of indexing that include different levels of indexes or simply multiple indexes. The DataFrame is classified under multiple indexes and the topmost index layer is presented as level 0 of the multilevel index followed by level 1, level 2, and so on.

Using loc[] property in multiindex dataframes

The loc[] property is a type of data selection method which takes the name of a row or column as a parameter. To perform various operations using the loc[] property, we need to pass the required condition of rows and columns to get the filtered data.

Based on a certain condition, we can filter the DataFrame values, and also we can update these values, hence loc[] method is also useful in updating values when a certain condition is satisfied, anyways, we are going to learn how to filter multiindex DataFrame with loc[].

Let us understand with the help of an example,

Python program to demonstrate how to use .loc with a MultiIndex in pandas

# Importing pandas package
import pandas as pd

# Creating multilevel index
index = pd.MultiIndex.from_tuples([
    ('Vitamin A','Sources'),
    ('Vitamin C', 'Sources'),
    ('Vitamin D','Sources')

# Creating a multilevel index DataFrame 
# with columns = multilevel indexes
df = pd.DataFrame([
    ['Papaya','Orange','Oily Fish'],
    ['Watermelon','Blackcurrent','Red meat'],
    ['Mango','Kale','egg yolks']
    ], columns=index)

# Display multilevel DataFrame
print("Multilevel DataFrame:\n",df,"\n")

# Selection based on loc condition
result = df.loc[([0,2]), 'Vitamin D']

# Display selected DataFrame
print("Selected DataFrame:\n",result)


The output of the above program is:

Example: Using .loc with a MultiIndex in pandas

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.