Percent of Total

Python · GroupBy & SQL-Style Analytics

Theory

Share of total is a value divided by a total, and the only question is which total. Share of the grand total is a simple vectorised division: df['amount'] / df['amount'].sum() * 100. Share within a group uses transform so every row is divided by its own group's total: df['amount'] / df.groupby('city')['amount'].transform('sum') * 100. This mirrors SQL's amount / SUM(amount) OVER (PARTITION BY city).

For a summary table, aggregate first and divide the result by its own sum: totals = df.groupby('city')['amount'].sum(); totals / totals.sum(). For counts, value_counts(normalize=True) gives proportions directly, and pd.crosstab(a, b, normalize='index') gives row percentages of a cross-tab. Round only for display (.round(1)) and check that each set of shares adds up to 100. Sorting by share and applying cumsum() gives a cumulative share, used for 80/20 (Pareto) analysis.

Why we use it

Percentages make different-sized groups comparable and answer questions like 'which city brings most of our revenue' or 'what share of each city's sales comes from each customer'.

When to use it

Divide by sum() for share of grand total, by groupby().transform('sum') for share within group, and use value_counts(normalize=True) for category proportions. Keep the unrounded numbers for further maths.

How it works

  1. Choose the denominator
  2. Share of the grand total? Yes: Divide by df['col'].sum(). No: Share within each group.
  3. Group total for every row: groupby(key)[col].transform('sum')
  4. Divide each value by its group total, multiply by 100
  5. Round for display and check the totals are 100
  6. Percentage column

Syntax

df['col'] / df['col'].sum() * 100
df['col'] / df.groupby('key')['col'].transform('sum') * 100
totals = df.groupby('key')['col'].sum(); totals / totals.sum()
s.value_counts(normalize=True)

Example

df['pct_of_city'] = (df['amount'] / df.groupby('city')['amount'].transform('sum') * 100).round(1)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic