How to get first and last values in a groupby?

Learn, how to get the first and last values in a group by object? 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 with two columns and some values, we need to perform groupby operation on this DataFrame and then we will apply aggregate function on this object and pass the aggregate first and last parameters in a list.

Getting the first and last values in a groupby

For this purpose, we will use df.groupby() method by passing level=0 and then use the .agg() method with a list of two parameters ['first', 'last'] on the result of the first statement.

The groupby() method 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. This method splits the object, apply some operations, and then combines them to create a group hence large amounts of data and computations can be performed on these groups.

Let us understand with the help of an example,

Python program to get first and last values in a groupby

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a DataFrame
df = pd.DataFrame(np.arange(20).reshape(10, -1),
    [['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'],
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']],
    ['X', 'Y'])

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# Performing groupby on df
gp = df.groupby(level=0)

# Applying aggregate function
res = gp.agg(['first', 'last'])

# Using stack method
res = res.stack()

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

Output

The output of the above program is:

Example: Get first and last values in a groupby?

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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