Pandas

Pandas GroupBy

01 · Why GroupBy? File · GroupBy · 1 of 8

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:

  1. Total sales by city
  2. Average salary by department
  3. Number of customers by job
  4. Max loan amount by branch
  5. Average balance by age group
  6. Total transactions by customer
02 · Sample Data File · GroupBy · 2 of 8

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.
03 · What is GroupBy? File · GroupBy · 3 of 8

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:

  1. Divide the data into groups based on one or more columns
  2. Perform some calculation on each group
  3. Combine the results

df.groupby("column_name")

Normally we combine groupby() with an aggregate function.

04 · Basic GroupBy File · GroupBy · 4 of 8

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()
05 · Aggregation Functions File · GroupBy · 5 of 8

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.
06 · Multiple Aggregations File · GroupBy · 6 of 8

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.
07 · GroupBy with Multiple Columns File · GroupBy · 7 of 8

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.
08 · Summary File · GroupBy · 8 of 8

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
Home Pandas GroupBy

Pandas GroupBy & Aggregation

Divide data into groups and perform calculations on each group.

Select any topic from the left sidebar to begin.


Start Learning →