GroupBy

Pandas Basics

1 min read

Published Sep 29 2025, updated Oct 24 2025


21
0
0
0

PandasPython

What is groupby?

  • A method to split data into groups based on some key(s).
  • Perform aggregations, transformations, or custom functions on each group.
  • Combine results into a new DataFrame or Series.





Basic Syntax

df.groupby(by)[column].operation()

  • by → column(s) or index level(s) to group on.
  • operation → aggregation (sum, mean, count, etc.), transformation, or apply.





Aggregation

Common built-ins:

  • .mean() → average per group
  • .sum() → sum per group
  • .count() → count non-NA entries per group
  • .size() → group sizes (including NAs)
  • .max() / .min() → extrema per group
  • .median(), .std(), .var()

Example:

import pandas as pd

data = {
    "Department": ["Sales", "Sales", "IT", "IT", "HR"],
    "Salary": [50000, 60000, 70000, 80000, 45000]
}
df = pd.DataFrame(data)

df.groupby("Department")["Salary"].mean()

Output:

Department
HR 45000
IT 75000
Sales 55000
Name: Salary, dtype: int64





Multiple Aggregations

Use .agg() for flexibility:

df.groupby("Department")["Salary"].agg(["mean", "sum", "max"])


Or custom names:

df.groupby("Department")["Salary"].agg(
    avg_salary="mean", total_salary="sum"
)





Grouping by Multiple Columns

df.groupby(["Department", "Salary"]).size()

This produces a MultiIndex.






Transformations

Unlike aggregation (reduces groups), transformations return the same shape as the original DataFrame.
Example: normalise salaries within departments:

df["Salary_normalized"] = df.groupby("Department")["Salary"].transform(lambda x: x / x.mean())





Filtering Groups

Use .filter() to drop groups based on a condition:

df.groupby("Department").filter(lambda x: x["Salary"].mean() > 60000)

Keeps only departments with an average salary above 60,000.






Iterating Over Groups

for name, group in df.groupby("Department"):
    print(name)
    print(group)

Returns multiple DataFrames:

  • name → the group key (value of the column you grouped by).
  • group → a DataFrame containing only the rows that belong to that group.

If you group by multiple columns, name becomes a tuple of keys.


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact