Last modified: Feb 04, 2026 By Alexander Williams

Calculate Percentages in Python with Pandas

Data analysis often requires percentage calculations. You might need growth rates or composition analysis. Python's Pandas library provides powerful tools for this. This guide explains the key functions.

What is the Pandas Percentage Function?

Pandas does not have one single "percentage function". Instead, it offers methods to compute different percentage metrics. The two most important are pct_change() and simple division. Understanding Python function syntax helps you use these effectively.

The pct_change() method calculates the percentage change between consecutive elements. It is perfect for time series data. You can see month-over-month or year-over-year growth.

Using pct_change() for Growth Rates

Let's start with a basic example. We will create a simple Series of sales data.


import pandas as pd

# Create a Series of monthly sales
sales_data = pd.Series([200, 220, 210, 250, 300])
print("Monthly Sales Data:")
print(sales_data)
    

Monthly Sales Data:
0    200
1    220
2    210
3    250
4    300
dtype: int64
    

Now, apply the pct_change() function to see the growth rate from one month to the next.


# Calculate percentage change
growth_rates = sales_data.pct_change()
print("\nMonth-over-Month Growth Rates:")
print(growth_rates)
    

Month-over-Month Growth Rates:
0         NaN
1    0.100000
2   -0.045455
3    0.190476
4    0.200000
dtype: float64
    

The first value is NaN because there is no previous data point. From month 0 to month 1, sales increased by 10%. Month 2 saw a decrease of about 4.55%.

Calculate Percentage of Total

Another common task is finding what percentage each part contributes to a whole. For this, we use the div() method or simple division.

Imagine we have a DataFrame with department budgets.


# Create a DataFrame
budget_df = pd.DataFrame({
    'Department': ['Marketing', 'Engineering', 'Sales', 'HR'],
    'Budget': [50000, 120000, 80000, 30000]
})
print("Department Budgets:")
print(budget_df)
    

Department Budgets:
     Department  Budget
0    Marketing   50000
1  Engineering  120000
2        Sales   80000
3           HR   30000
    

To find each department's percentage of the total budget, divide the 'Budget' column by the sum of all budgets.


# Calculate total budget
total_budget = budget_df['Budget'].sum()

# Calculate percentage composition
budget_df['Budget_Percentage'] = (budget_df['Budget'] / total_budget) * 100
print("\nBudgets with Percentage of Total:")
print(budget_df)
    

Budgets with Percentage of Total:
     Department  Budget  Budget_Percentage
0    Marketing   50000          17.857143
1  Engineering  120000          42.857143
2        Sales   80000          28.571429
3           HR   30000          10.714286
    

Engineering uses 42.86% of the total budget. HR uses 10.71%. This is a clear view of resource allocation.

Advanced Usage: pct_change() with DataFrames

The pct_change() method works on entire DataFrames. This is useful for financial data with multiple columns. You can calculate changes across different periods.


# Create a DataFrame with quarterly revenue for products
revenue_df = pd.DataFrame({
    'Q1': [150, 90],
    'Q2': [165, 100],
    'Q3': [180, 95],
    'Q4': [200, 110]
}, index=['Product_A', 'Product_B'])
print("Quarterly Revenue:")
print(revenue_df)
    

Quarterly Revenue:
           Q1   Q2   Q3   Q4
Product_A  150  165  180  200
Product_B   90  100   95  110
    

Calculate the quarter-over-quarter growth for both products. The axis parameter is key here. Setting axis=1 calculates change across columns (quarters). Understanding function argument unpacking can help with such parameters.


# Calculate percentage change across columns (quarters)
quarterly_growth = revenue_df.pct_change(axis=1)
print("\nQuarter-over-Quarter Growth (%):")
print(quarterly_growth)
    

Quarter-over-Quarter Growth (%):
                 Q1        Q2        Q3        Q4
Product_A       NaN  0.100000  0.090909  0.111111
Product_B       NaN  0.111111 -0.050000  0.157895
    

Product_A grew by 10% from Q1 to Q2. Product_B had a 5% decline from Q2 to Q3. This analysis helps spot trends quickly.

Handling Missing Data in Percentage Calculations

Real-world data often has gaps. The pct_change() method has a fill_method parameter. You can forward-fill or back-fill missing values before calculation.

Always check your data for NaNs before calculating percentages. They can distort your results.

Practical Tips for Percentage Calculations

Here are key tips for using Pandas percentage functions.

First, know your goal. Use pct_change() for sequential differences. Use division for composition or contribution to a total.

Second, format your results. After calculation, you can round the percentages for readability.


# Round the percentage composition to 2 decimal places
budget_df['Budget_Percentage'] = budget_df['Budget_Percentage'].round(2)
print("\nFormatted Budget Percentages:")
print(budget_df[['Department', 'Budget_Percentage']])
    

Formatted Budget Percentages:
     Department  Budget_Percentage
0    Marketing               17.86
1  Engineering               42.86
2        Sales               28.57
3           HR               10.71
    

Third, visualize your results. A pie chart is excellent for percentage-of-total data. A line chart works well for growth rate trends.

Conclusion

Calculating percentages is a core data analysis skill. Pandas provides efficient methods like pct_change() and simple arithmetic. You can analyze growth trends and