How to groupby consecutive values in pandas dataframe?

Given a pandas dataframe, we have to groupby consecutive values in pandas dataframe. 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

If we are given a DataFrame that contains a column with multiple values and we need to group every consecutive value. For example, suppose we have a column as follows:
[2,4,6,8,10,12]
And we need to group them like this:
[2] [4] [6] [8] [10] [12]

Groupby consecutive values in pandas dataframe

For this purpose, we will use the groupby() method of the itertools library. What we will do is we will access all the values of DataFrame, convert them into a list using the tolist() method, and then group them.

Let us understand with the help of an example,

Python program to groupby consecutive values in pandas dataframe

# Importing pandas package
import pandas as pd

# Importing groupby method from itertools
from itertools import groupby

# Creating a dictionary
d = {'a':[2,4,6,8,10,12]}

# Creating DataFrame
df = pd.DataFrame(d)

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

# getting values and grouping them
res = [ list(group) for key, group in groupby(df.a.values.tolist())]

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

Output

The output of the above program is:

Example: Groupby consecutive values in pandas dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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