Member-only story
Pandas Tricks - Calculate Percentage Within Group
Pandas groupby probably is the most frequently used function whenever you need to analyse your data, as it is so powerful for summarizing and aggregating data. Often you still need to do some calculation on your summarized data, e.g. calculating the % of vs total within certain category. In this article, I will be sharing with you some tricks to calculate percentage within groups of your data.
Prerequisite
You will need to install pandas if you have not yet installed:
pip install pandas
#or conda install pandas
I am going to use some real world example to demonstrate what kind of problems we are trying to solve. The sample data I am using is from this link , and you can also download it and try by yourself.
Let’s first read the data from this sample file:
import pandas as pd
# You can also replace the below file path to the URL of the file
df = pd.read_excel(r"C:\Sample Sales Data.xlsx", sheet_name="Sheet")
The data will be loaded into pandas dataframe, you will be able to see something as per below: