pandas
groupby
data analysis
percentage calculation
python programming

Pandas percentage of total with groupby

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Pandas is an essential data manipulation library in Python that simplifies data analysis. One common task is calculating the percentage contribution of different groups to a total. The combination of Pandas' groupby function with arithmetic operations makes this straightforward and efficient. This article walks through multiple approaches with detailed examples so you can pick the one that fits your workflow.

Understanding groupby

The groupby function splits data into groups based on column values, applies a function to each group, and combines the results. It works similarly to SQL's GROUP BY clause.

Example Dataset

Let's create a simple DataFrame to work with throughout this article.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    'Category': ['A', 'B', 'A', 'B', 'C', 'A', 'C'],
5    'Values': [100, 150, 200, 250, 300, 350, 400]
6})
7
8print(df)

Output:

 
1  Category  Values
20        A     100
31        B     150
42        A     200
53        B     250
64        C     300
75        A     350
86        C     400

Method 1: Step-by-Step Calculation

The most readable approach calculates the group totals first, then divides by the overall total.

python
1# Step 1: Calculate the sum per group
2group_totals = df.groupby('Category')['Values'].sum()
3
4# Step 2: Calculate the overall total
5overall_total = df['Values'].sum()
6
7# Step 3: Compute the percentage
8percentage = (group_totals / overall_total) * 100
9
10print(percentage)

Output:

 
1Category
2A    35.135135
3B    21.621622
4C    37.837838
5Name: Values, dtype: float64

This tells you that Category A accounts for about 35.1% of the total, Category B for about 21.6%, and Category C for about 37.8%.

Method 2: Using transform for Row-Level Percentages

Sometimes you need the percentage attached to every row in the original DataFrame, not just a summary. The transform method broadcasts the group operation back to the original index.

python
df['Percentage'] = df['Values'] / df.groupby('Category')['Values'].transform('sum') * 100

print(df)

Output:

 
1  Category  Values  Percentage
20        A     100   15.384615
31        B     150   37.500000
42        A     200   30.769231
53        B     250   62.500000
64        C     300   42.857143
75        A     350   53.846154
86        C     400   57.142857

In this case, each row shows what percentage it contributes to its own category total. If you want each row's percentage of the overall total instead, skip the groupby in the denominator.

python
df['PctOfTotal'] = df['Values'] / df['Values'].sum() * 100

Method 3: Method Chaining

Pandas supports method chaining for concise, expressive code. This one-liner computes the percentage of each category relative to the grand total.

python
1result = (
2    df.groupby('Category')['Values']
3    .sum()
4    .div(df['Values'].sum())
5    .mul(100)
6    .round(2)
7)
8
9print(result)

Output:

 
1Category
2A    35.14
3B    21.62
4C    37.84
5Name: Values, dtype: float64

The div and mul methods read more naturally in a chain than using / and * operators.

Method 4: Using value_counts with normalize

For simple frequency-based percentages (how often each category appears, not summed values), value_counts with normalize=True is the fastest option.

python
freq_pct = df['Category'].value_counts(normalize=True) * 100
print(freq_pct)

Output:

 
1Category
2A    42.857143
3C    28.571429
4B    28.571429
5Name: proportion, dtype: float64

This shows that Category A appears in about 42.9% of the rows.

Multiple Group Columns

When your data has multiple grouping columns, the same pattern applies. Here is an example with two levels of grouping.

python
1df2 = pd.DataFrame({
2    'Region': ['East', 'East', 'West', 'West', 'East', 'West'],
3    'Category': ['A', 'B', 'A', 'B', 'A', 'B'],
4    'Sales': [100, 200, 150, 250, 300, 350]
5})
6
7group_totals = df2.groupby(['Region', 'Category'])['Sales'].sum()
8overall_total = df2['Sales'].sum()
9percentage = (group_totals / overall_total * 100).round(2)
10
11print(percentage)

This gives you the percentage breakdown across every combination of Region and Category.

Common Pitfalls

  • Missing values: NaN values in the grouping column cause those rows to be excluded from group operations by default. Use fillna before grouping if you want to include them, or pass dropna=False to groupby.
  • Data type issues: If your value column contains strings or mixed types, arithmetic will fail. Convert with pd.to_numeric(df['Values'], errors='coerce') before calculating.
  • Rounding errors: Percentages may not sum to exactly 100 due to floating-point arithmetic. Use .round(2) for display and accept minor rounding differences.
  • Confusing transform and agg: agg returns a reduced DataFrame (one row per group), while transform returns a Series with the same index as the original. Use transform when you need to merge the result back into the original DataFrame without an explicit join.

Summary

Calculating percentage of total with Pandas groupby can be done in several ways. Use the step-by-step approach for clarity, transform when you need row-level percentages, method chaining for concise code, and value_counts(normalize=True) for frequency-based percentages. For multi-column grouping, the same patterns extend naturally. Always handle missing values and data types before performing calculations to avoid unexpected results.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.