GroupBy
GroupBy allows you to divide a DataFrame into groups based on a column and then perform calculations on each group. It is one of the most useful Pandas features for analyzing real-world datasets.
GroupBy means: Group → Calculate → Understand.
Imagine you have sales from many cities. Instead of looking at every sale individually, GroupBy lets you group the sales by city and calculate things such as total sales, average sales, or number of orders for each city.
Start With a DataFrame
Let's create a simple sales dataset.
import pandas as pd
sales = pd.DataFrame({
"City": [
"Hyderabad",
"London",
"Hyderabad",
"New York",
"London",
"Hyderabad"
],
"Product": [
"Laptop",
"Phone",
"Phone",
"Laptop",
"Laptop",
"Tablet"
],
"Sales": [
1200,
800,
600,
1500,
1000,
700
]
})
print(sales)
City Product Sales 0 Hyderabad Laptop 1200 1 London Phone 800 2 Hyderabad Phone 600 3 New York Laptop 1500 4 London Laptop 1000 5 Hyderabad Tablet 700
Notice that some cities appear more than once. Hyderabad appears three times and London appears twice.
Why Do We Need GroupBy?
Suppose you want to answer this question:
How much did each city sell?
Looking at the original DataFrame manually would be annoying, especially when there are thousands or millions of rows.
GroupBy solves this problem by putting rows belonging to the same city together.
Hyderabad
1200
600
700
London
800
1000
New York
1500
Now we can calculate something for each group.
Basic GroupBy
The basic syntax is:
df.groupby("column")
For our sales data:
groups = sales.groupby("City")
print(groups)
At this point, Pandas has created groups, but we haven't asked it to calculate anything yet.
This is an important point: GroupBy by itself is usually only the first step.
GroupBy With sum()
Now let's calculate the total sales for each city.
result = sales.groupby("City")["Sales"].sum()
print(result)
City Hyderabad 2500 London 1800 New York 1500 Name: Sales, dtype: int64
Let's understand the calculation:
Hyderabad 1200 + 600 + 700 = 2500 London 800 + 1000 = 1800 New York 1500 = 1500
So GroupBy has converted six individual sales records into three city-level results.
GroupBy With mean()
Instead of total sales, maybe we want the average sale for each city.
result = sales.groupby("City")["Sales"].mean()
print(result)
City Hyderabad 833.333333 London 900.000000 New York 1500.000000 Name: Sales, dtype: float64
For Hyderabad:
(1200 + 600 + 700) / 3 = 833.33
This tells us the average transaction value for each city.
GroupBy With count()
You can also count how many records belong to each group.
result = sales.groupby("City")["Sales"].count()
print(result)
City Hyderabad 3 London 2 New York 1 Name: Sales, dtype: int64
Now we know:
Hyderabad → 3 sales London → 2 sales New York → 1 sale
GroupBy With min() and max()
You can find the smallest and largest value in each group.
result = sales.groupby("City")["Sales"].max()
print(result)
City Hyderabad 1200 London 1000 New York 1500 Name: Sales, dtype: int64
To find the minimum:
result = sales.groupby("City")["Sales"].min()
The same GroupBy structure can therefore answer different questions depending on the aggregation function.
Multiple Calculations With agg()
Sometimes you want several calculations at the same time.
result = sales.groupby("City")["Sales"].agg([
"sum",
"mean",
"count",
"min",
"max"
])
print(result)
sum mean count min max City Hyderabad 2500 833.333333 3 600 1200 London 1800 900.000000 2 800 1000 New York 1500 1500.000000 1 1500 1500
One operation now gives us several useful statistics.
GroupBy Multiple Columns
You can group by more than one column.
For example, group sales by both City and Product.
result = sales.groupby(
["City", "Product"]
)["Sales"].sum()
print(result)
City Product
Hyderabad Laptop 1200
Phone 600
Tablet 700
London Laptop 1000
Phone 800
New York Laptop 1500
Name: Sales, dtype: int64
Now Pandas creates a group for every City + Product combination.
Keep Group Columns as Normal Columns
By default, GroupBy often places the grouping column in the result index.
You can keep it as a normal column using
as_index=False.
result = sales.groupby(
"City",
as_index=False
)["Sales"].sum()
print(result)
City Sales 0 Hyderabad 2500 1 London 1800 2 New York 1500
This format is often easier to work with because
City remains a normal DataFrame column.
Group Multiple Columns and Calculate Different Values
You can also perform different calculations on different columns.
result = sales.groupby(
"City",
as_index=False
).agg(
total_sales=("Sales", "sum"),
average_sales=("Sales", "mean"),
number_of_sales=("Sales", "count")
)
print(result)
City total_sales average_sales number_of_sales 0 Hyderabad 2500 833.333333 3 1 London 1800 900.000000 2 2 New York 1500 1500.000000 1
This is a very useful pattern for real-world data analysis because you can give the resulting columns meaningful names.
GroupBy With Filtering
You can also use the result of GroupBy to find groups that meet a condition.
For example, find cities with total sales greater than 1800.
result = sales.groupby(
"City",
as_index=False
)["Sales"].sum()
result = result[
result["Sales"] > 1800
]
print(result)
City Sales 0 Hyderabad 2500
The important workflow is:
Raw Data ↓ Group by City ↓ Calculate total sales ↓ Filter totals > 1800 ↓ Final result
Real-World AI Example
Imagine you are analyzing customer data:
Customer Country Spending Raj India 1500 John UK 900 Sarah India 2200 David USA 1800 Emma UK 1200
You might want to know the average customer spending for each country.
customers.groupby(
"Country"
)["Spending"].mean()
This changes the question from:
"How much did each customer spend?"
to:
"What is the average spending for each country?"
That type of aggregation is extremely common in data analysis and Machine Learning preparation.
Simple Mental Model
Think of GroupBy like putting students into groups
Students Raj → Hyderabad John → London Sarah → Hyderabad David → London Emma → Hyderabad
Group them by city:
Hyderabad → Raj → Sarah → Emma London → John → David
Then calculate something for each group:
Hyderabad → 3 students London → 2 students
That is the basic idea behind Pandas GroupBy.
Remember This Pattern
Most GroupBy operations follow this simple pattern:
df.groupby("column")["value_column"].function()
For example:
# Total
df.groupby("City")["Sales"].sum()
# Average
df.groupby("City")["Sales"].mean()
# Count
df.groupby("City")["Sales"].count()
# Minimum
df.groupby("City")["Sales"].min()
# Maximum
df.groupby("City")["Sales"].max()
Once you understand this pattern, most basic GroupBy operations become straightforward.
GroupBy lets you analyze data group by group.
Use groupby() to create groups and then use
functions such as sum(),
mean(), count(),
min(), max(), or
agg() to calculate information for each
group. The easiest way to remember it is:
Group → Calculate → Understand.