0
0
Pandasdata~5 mins

Iterating over groups in Pandas

Choose your learning style9 modes available
Introduction

We use iterating over groups to look at parts of data separately. It helps us understand or work with each group one by one.

You want to analyze sales data for each store separately.
You need to calculate average scores for each class in a school dataset.
You want to process customer data grouped by region.
You want to apply different operations to each group in your data.
Syntax
Pandas
for group_name, group_data in df.groupby('column_name'):
    # do something with group_name and group_data

group_name is the value of the group (like a category).

group_data is a smaller DataFrame with only rows from that group.

Examples
This prints each city name and the rows for that city.
Pandas
for city, data in df.groupby('City'):
    print(city)
    print(data)
This calculates and prints the average price for each category.
Pandas
for category, group in df.groupby('Category'):
    avg_price = group['Price'].mean()
    print(f"Average price in {category}: {avg_price}")
Sample Program

This program groups sales data by city and sums sales for each city. Then it prints the total sales per city.

Pandas
import pandas as pd

data = {'City': ['Paris', 'Paris', 'London', 'London', 'Berlin'],
        'Sales': [100, 150, 200, 250, 300]}
df = pd.DataFrame(data)

for city, group in df.groupby('City'):
    total_sales = group['Sales'].sum()
    print(f"Total sales in {city}: {total_sales}")
OutputSuccess
Important Notes

Group order depends on the sorting of the group keys.

You can group by multiple columns by passing a list, like df.groupby(['City', 'Category']).

Summary

Use groupby to split data into groups.

Iterate over groups with a for loop to work on each group separately.

Each group gives you the group name and the data rows for that group.