How to use Dataframe.groupby() and Groupby_object.groups.keys() together In Python
Groupby_object.groups.keys() method will return the keys of the groups.
Python3
# import pandas library import pandas as pd # dictionary dict = { 'X' : [ 'A' , 'B' , 'A' , 'B' ], 'Y' : [ 1 , 4 , 3 , 2 ]} # create a dataframe df = pd.DataFrame( dict ) # group by "X" column groups = df.groupby( 'X' ) # extract keys from groups keys = groups.groups.keys() for i in keys: print (groups.get_group(i)) print ( '\n' ) |
Output:
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.
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:
Python3
# import pandas library import pandas as pd # dictionary dict = { 'X' : [ 'A' , 'B' , 'A' , 'B' ], 'Y' : [ 1 , 4 , 3 , 2 ]} # create a dataframe df = pd.DataFrame( dict ) # show the dataframe df |
Output:
Contact Us