How to Iterate over Dataframe Groups in Python-Pandas? Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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: Iterate over Data frame Groups in Python-PandasUsing DataFrame.groupby() to Iterate over Data frame Groups DataFrame.groupby() function in Python is used to split the data into groups based on some criteria. 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") for name, group in groups: print(name) print(group) print("\n") Output: Iterate over Data frame Groups in Python-Pandas 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. 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: Iterate over Data frame Groups in Python-Pandas 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. Create Quiz Comment P parasmadan15 Follow 1 Improve P parasmadan15 Follow 1 Improve Article Tags : Python Python-pandas Python pandas-dataFrame Python Pandas-exercise Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like