Skip to main content

Group By in Pandas

Calculating read time…

Powerful Aggregation

If you're learning pandas, GroupBy is the feature that will make you feel like a real data analyst. It lets you split your data into groups (e.g., by department, city, or category), apply calculations (like sum, mean, count), and combine the results — all in just a few lines of code.

This "split-apply-combine" approach is incredibly useful for real-world tasks like:

  • Sales summary by region or product
  • Average salary by department
  • Customer behavior by age group
  • Exam scores by class or gender

Our Sample DataFrame

We'll use a simple company employee dataset:

import pandas as pd

data = {
    'Department': ['Sales', 'Sales', 'Engineering', 'Engineering', 'HR', 'HR', 'Sales', 'Engineering'],
    'Employee': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Heidi'],
    'Salary': [60000, 65000, 90000, 95000, 55000, 58000, 62000, 98000],
    'Experience_Years': [3, 5, 8, 10, 4, 6, 4, 12]
}

df = pd.DataFrame(data)
print(df)

This will display:

Department Employee Salary Experience_Years
0 Sales Alice 60000 3
1 Sales Bob 65000 5
2 Engineering Charlie 90000 8
3 Engineering David 95000 10
4 HR Eve 55000 4
5 HR Frank 58000 6
6 Sales Grace 62000 4
7 Engineering Heidi 98000 12

Basic GroupBy: Average Salary by Department

The simplest way: group by a column and apply an aggregation like mean(), sum(), or count().

# Average salary per department
avg_salary = df.groupby('Department')['Salary'].mean()
print(avg_salary)

Output:

Department Salary
Engineering 94333.33
HR 56500.00
Sales 62333.33

Multiple Aggregations at Once

Use agg() (or aggregate()) to get several stats together.

# Multiple stats for Salary and Experience
summary = df.groupby('Department').agg({
    'Salary': ['mean', 'sum', 'count'],
    'Experience_Years': ['mean', 'max']
})

print(summary)

Output:

Department Salary Experience_Years
mean sum count mean max
Engineering 94333.33 283000 3 10.00 12
HR 56500.00 113000 2 5.00 6
Sales 62333.33 187000 3 4.00 5

Common Aggregations (Quick Cheat Sheet)

  • .mean() — Average
  • .sum() — Total
  • .count() — Number of rows
  • .min() / .max() — Lowest/Highest
  • .std() — Standard deviation
  • .size() — Count including NaN

Key Tips for Beginners

  • GroupBy returns a GroupBy object — always apply an aggregation function afterward.
  • Use .reset_index() if you want a regular DataFrame back.
  • You can group by multiple columns: df.groupby(['Col1', 'Col2']).
  • Perfect for exploratory data analysis (EDA) — run GroupBy early to spot patterns!

Happy Learning !! 🐼

Comments