Python - Pandas conditional rolling count

Learn about the Pandas conditional rolling count with examples.
Submitted by Pranit Sharma, on August 21, 2022

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

In this article, we are going to learn how to apply conditional rolling count logic in pandas dataframe?

Pandas conditional rolling count

By rolling count, for each row, we will count how many times the value has appeared consecutively. For this process, we will first apply the groupby() method so that all the similar values can be grouped.

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. It splits the object, apply some operations, and then combines them to create a group. Hence a large amount of data and computations can be performed on these groups.

After grouping the values, we will use the cumcount() method which will return the Sequence number of each element within each group.

Let us understand with the help of an example,

Python program to apply conditional rolling count logic in pandas dataframe

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {'Col':[1,1,1,2,2,3,3,3,4,4]}

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

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


# Finding rolling count
df['Count'] = df.groupby((df['Col'] != df['Col'].shift(1)).cumsum()).cumcount()+1

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Pandas conditional rolling count

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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