Pandas difference between largest and smallest value within group

Given a pandas dataframe, we have to find the difference between largest and smallest value within group. 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

We are given a DataFrame with two columns, group and value, we need to find the max value of the group and the min value of the group and compute the difference between these two.

Finding the difference between largest and smallest value within group

The Max value is the largest of all the values and the min value is the smallest of all the values in a column. We will store this difference in a new column called the difference.

Let us understand with the help of an example,

Python program to find the difference between largest and smallest value within group

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'group':[1,2,1,2,1],
    'value':[20,2,30,7,14]
}

# Creating a DataFrame
df = pd.DataFrame(d)

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

# Getting the diff of max and min
res = df.groupby('group')['value'].agg(np.ptp)

# Display Modified DataFrame
print('Modified DataFrame:\n',res)

Output

The output of the above program is:

Example: Pandas difference between largest and smallest value within group

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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