We use groupby() to split data into groups based on some criteria. This helps us analyze each group separately.
0
0
groupby() basics in Pandas
Introduction
You want to find the average sales per store from a sales list.
You need to count how many students are in each class from a school dataset.
You want to sum expenses by category from your budget data.
You want to see the maximum temperature recorded each day from weather data.
Syntax
Pandas
df.groupby('column_name')This creates groups based on unique values in the specified column.
You usually follow it with an aggregation like .sum(), .mean(), or .count().
Examples
Groups data by 'Category' and sums numeric columns in each group.
Pandas
df.groupby('Category').sum()
Groups data by 'Class' and calculates the average of numeric columns for each class.
Pandas
df.groupby('Class').mean()Groups data by 'Store' and counts the number of rows in each group.
Pandas
df.groupby('Store').count()Sample Program
This code creates a small table of sales by store. Then it groups the data by 'Store' and sums the sales for each store. Finally, it prints the total sales per store.
Pandas
import pandas as pd data = {'Store': ['A', 'B', 'A', 'B', 'C'], 'Sales': [100, 200, 150, 300, 250]} df = pd.DataFrame(data) grouped = df.groupby('Store').sum() print(grouped)
OutputSuccess
Important Notes
Grouping does not change the original data unless you assign or print the result.
You can group by multiple columns by passing a list, like df.groupby(['Col1', 'Col2']).
Summary
groupby() splits data into groups based on column values.
Use aggregation functions like sum(), mean(), or count() after grouping.
This helps analyze parts of data separately and find patterns.