Profit Margin Analysis Project with Python

Data Analytics project 15

Profit Margin Analysis Project with Python

Build a complete profit margin analysis portfolio project with documented metrics, reproducible Python code, validation checks, and responsible interpretation.

Explore Data Analytics training in VizagView all project ideas

Business question

Which products generate revenue, contribution, and operating profit after the chosen cost allocation?

Dataset and grain

Twelve months of synthetic product units, price, unit variable cost, and allocated fixed cost for three products.

Requirements

Python 3.10 or later with numpy and pandas installed.

Method and validation checks

Calculate revenue and variable cost from units, derive contribution, subtract allocated fixed cost, aggregate by product, and calculate two margin rates.

  • Revenue equals units times price
  • Contribution reconciles to revenue minus variable cost
  • Operating profit reconciles after allocated fixed cost
  • Allocation assumptions are disclosed with the ranking

Complete Python code

Save the code as da_profit_margin_analysis.py. Review the stated model and field assumptions before using another dataset.

"""Calculate revenue, gross margin, contribution margin, and operating margin."""

from __future__ import annotations

import numpy as np
import pandas as pd


def make_demo_profit_data(seed: int = 42) -> pd.DataFrame:
    rng = np.random.default_rng(seed)
    months = pd.date_range("2025-01-01", periods=12, freq="MS")
    products = ["Core", "Plus", "Premium"]
    rows = []
    for product in products:
        for month in months:
            units = rng.integers(350, 1300)
            price = {"Core": 900, "Plus": 1600, "Premium": 2800}[product]
            variable_cost = {"Core": 420, "Plus": 720, "Premium": 1180}[product]
            rows.append((month, product, units, price, variable_cost, rng.uniform(90_000, 220_000)))
    return pd.DataFrame(rows, columns=["month", "product", "units", "unit_price", "unit_variable_cost", "allocated_fixed_cost"])


def analyse_margins(data: pd.DataFrame) -> pd.DataFrame:
    result = data.copy()
    result["revenue"] = result["units"] * result["unit_price"]
    result["variable_cost"] = result["units"] * result["unit_variable_cost"]
    result["contribution"] = result["revenue"] - result["variable_cost"]
    result["operating_profit"] = result["contribution"] - result["allocated_fixed_cost"]
    summary = result.groupby("product", as_index=False).agg(revenue=("revenue", "sum"), variable_cost=("variable_cost", "sum"), fixed_cost=("allocated_fixed_cost", "sum"), contribution=("contribution", "sum"), operating_profit=("operating_profit", "sum"))
    summary["contribution_margin"] = summary["contribution"] / summary["revenue"]
    summary["operating_margin"] = summary["operating_profit"] / summary["revenue"]
    return summary.sort_values("operating_margin", ascending=False)


def main() -> None:
    print(analyse_margins(make_demo_profit_data()).round(3).to_string(index=False))
    print("Allocated fixed costs can change product margins; document the allocation rule.")


if __name__ == "__main__":
    main()

Build and run the project

Run python da_profit_margin_analysis.py to calculate product contribution and operating margins.

  1. Confirm the source grain and field definitions.
  2. Reconcile record counts and additive totals.
  3. Validate rate denominators and date filters.
  4. Review outliers and missing values.
  5. Read the interpretation limits before sharing conclusions.

Expected analytical output

Product revenue, variable and fixed cost, contribution, operating profit, contribution margin, and operating margin.

Interpretation and responsible-use limits

Allocated fixed costs can materially change product-level profitability. Confirm cost behaviour, shared-cost allocation, returns, taxes, and transfer pricing with finance before action.

Ways to extend the project

Add price-volume-mix analysis, break-even units, scenario controls, customer profitability, period trends, and finance-system reconciliation.

Continue learning Data Analytics

Try the next project, return to the Softenant project library, or explore the Data Analytics course in Vizag for guided SQL, Excel, Power BI, Python, dashboard, and portfolio practice.