Pandas shift down values by one row within a group

Learn, how can we shift down values by one row within a group in Python pandas? By Pranit Sharma Last updated : October 03, 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.

Problem statement

Suppose, we are given a DataFrame and we need to create new columns whose values are that of another column, shifted down by one row.

Shifting down values by one row within a group

We will do this by groupby() and for this purpose, we will use df.groupby() method. The groupby() is a simple but very useful concept in pandas. By using groupby(), we can create a grouping of certain values and perform some operations on those values.

The groupby() method splits the object, applies some operations, and then combines them to create a group hence a large amount of data and computations can be performed on these groups.

The groupby operation packs the data into an object and if we want to check out that data, we need to access that object and we are going to get all the keys to this object.

Let us understand with the help of an example,

Python program to shift down values by one row within a group

# Importing pandas package
import pandas as pd

# Import numpy package
import  numpy as np

# Creating a dictionary
d = np.random.randint(1,3, (10,5))

# Creating a DataFrame
df = pd.DataFrame(d,columns=['A','B','C','D','E'])

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Grouping values
for k, v in df.groupby('A'):
    print("Key:",k,"\n")
    print('Original',"\n")
    print("Value:\n",v,"\n")
    print('Shifted',"\n")
    print(v.shift(1))

Output

The output of the above program is:

Example: Shift down values by one row within a group

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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