How to use pandas to get the percentage value of a column within a group?

Jack Dong
3 min readJun 15, 2024

To calculate the percentage value of a column within each group using pandas, you typically start by grouping the data based on one or more criteria and then calculate the percentage contribution of each item in the group to a summary statistic for that group (like sum, count, etc.). This is often done to understand the proportion or distribution of a quantity within subsets of your dataset.

Here’s a step-by-step example to show you how to calculate the percentage of sales for each product within each group (store, in this case).

Example Setup

Suppose you have a DataFrame representing sales data for different products across various stores.

Step 1: Create a Sample DataFrame

import pandas as pd

# Create a sample DataFrame
data = {
'Store': ['Store A', 'Store A', 'Store B', 'Store B', 'Store B', 'Store A'],
'Product': ['Apples', 'Bananas', 'Apples', 'Oranges', 'Bananas', 'Oranges'],
'Sales': [150, 200, 180, 300, 300, 150]
}
df = pd.DataFrame(data)

Step 2: Calculate the Total Sales per Store

First, calculate the total sales per store. This will help in calculating the percentage of each product’s…

--

--