How to Iterate over Dataframe Groups in Python-Pandas?
In this article, we’ll see how we can iterate over the groups in which a data frame is divided. So, let’s see different ways to do this task.
First, Let’s create a data frame:
Using DataFrame.groupby() to Iterate over Data frame Groups
In above example, we have grouped on the basis of column “X”. As there are two different values under column “X”, so our data frame will be divided into 2 groups. Then our for loop will run 2 times as the number groups are 2. “name” represents the group name and “group” represents the actual grouped data frame.
Using Dataframe.groupby() and Groupby_object.groups.keys() together
Groupby_object.groups.keys() method will return the keys of the groups.
In above example, we’ll use the function groups.get_group() to get all the groups. First we’ll get all the keys of the group and then iterate through that and then calling get_group() method for each key. get_group() method will return group corresponding to the key.