Pandas
Pandas GroupBy
Why GroupBy is Important?
Pandas groupby() and aggregation is one of the most important topics for data analysis.
Common business questions we can answer with groupby:
- Total sales by city
- Average salary by department
- Number of customers by job
- Max loan amount by branch
- Average balance by age group
- Total transactions by customer
Sample Data
Python
import pandas as pd
data = {
"employee": ["Kencha", "manu", "rahul", "priya", "Kiran", "Varshit"],
"Department": ["IT", "HR", "IT", "Finance", "HR", "Finance"],
"Salary": [60000, 45000, 70000, 80000, 50000, 75000]
}
df = pd.DataFrame(data)
df
Output
| employee | Department | Salary | |
|---|---|---|---|
| 0 | Kencha | IT | 60000 |
| 1 | manu | HR | 45000 |
| 2 | rahul | IT | 70000 |
| 3 | priya | Finance | 80000 |
| 4 | Kiran | HR | 50000 |
| 5 | Varshit | Finance | 75000 |
We created a small DataFrame with employee name, department and salary.
What is GroupBy?
Suppose I ask: What is the average salary in each department?
- Finance → Average salary
- HR → Average salary
- IT → Average salary
GroupBy means:
- Divide the data into groups based on one or more columns
- Perform some calculation on each group
- Combine the results
df.groupby("column_name")
Normally we combine groupby() with an aggregate function.
Basic GroupBy
Python
df.groupby("Department")["Salary"].mean()
Output
Department Finance 77500.0 HR 47500.0 IT 65000.0 Name: Salary, dtype: float64
This groups the data by Department and calculates the average salary of each group.
Python
df.groupby("Department")["Salary"].sum()
df.groupby("Department")["Salary"].max()
df.groupby("Department")["Salary"].min()
df.groupby("Department")["Salary"].count()
Output
Department Finance 155000 HR 95000 IT 130000 Name: Salary, dtype: int64 Department Finance 80000 HR 50000 IT 70000 Name: Salary, dtype: int64 Department Finance 75000 HR 45000 IT 60000 Name: Salary, dtype: int64 Department Finance 2 HR 2 IT 2 Name: Salary, dtype: int64
Common aggregation functions:
mean(), sum(), max(), min(), count()
Common Aggregation Functions
Python
df.groupby("Department").agg({
"Salary": "mean"
})
df.groupby("Department").agg({
"Salary": ["mean", "sum", "max", "min", "count"]
})
Output
Salary
Department
Finance 77500.0
HR 47500.0
IT 65000.0
mean sum max min count
Department
Finance 77500.0 155000 80000 75000 2
HR 47500.0 95000 50000 45000 2
IT 65000.0 130000 70000 60000 2
Using
.agg() we can apply one or more aggregation functions at the same time.
Multiple Aggregations
Python
df.groupby("Department").agg(
Average_Salary=("Salary", "mean"),
Total_Salary=("Salary", "sum"),
Max_Salary=("Salary", "max"),
Employee_Count=("Salary", "count")
)
Output
| Department | Average_Salary | Total_Salary | Max_Salary | Employee_Count |
|---|---|---|---|---|
| Finance | 77500.0 | 155000 | 80000 | 2 |
| HR | 47500.0 | 95000 | 50000 | 2 |
| IT | 65000.0 | 130000 | 70000 | 2 |
This is a clean way to give custom names to the aggregated columns.
GroupBy with Multiple Columns
We can also group by more than one column.
Python
df.groupby(["Department", "employee"])["Salary"].mean()
Output
Department employee
Finance Varshit 75000.0
priya 80000.0
HR Kiran 50000.0
manu 45000.0
IT Kencha 60000.0
rahul 70000.0
Name: Salary, dtype: float64
When we pass a list of columns to
groupby(), it creates multi-level groups.
Summary
groupby()divides data into groups- We usually apply aggregation functions like
mean(),sum(),count(),max(),min() - Use
.agg()for multiple aggregations - We can group by one or more columns
Pandas GroupBy & Aggregation
Divide data into groups and perform calculations on each group.
Select any topic from the left sidebar to begin.
Start Learning →
