How to shift Pandas DataFrame with a multiindex?

Given a pandas dataframe, we have to shift it with a multiindex. By Pranit Sharma Last updated : October 05, 2023

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.

Multilevel 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.

Problem statement

Suppose we are given a data frame with some multiindex and we need to shift a column based on the index without having Pandas assign the shifted value to a different index value.

Shiftting Pandas DataFrame with a multiindex

For this purpose, we will simply use groupby() and apply the shift() method to this group by the object.

Let us understand with the help of an example,

Python program to shift Pandas DataFrame with a multiindex

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating multilevel index
index = ['Vitamin A','Vitamin C','Vitamin D']

# 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")

# Shifting dataframe
res = df['Vitamin A_shifted'] = df.groupby(level=0)['Vitamin A'].shift(1)

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: How to shift Pandas DataFrame with a multiindex?

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.