In Python, load the data with pandas and compute contribution margin from column totals for the whole period, or row by row for each month. On the sample data it returns 10.01.
Contribution margin = (Revenue − COGS − Shipping − Marketing) ÷ Revenue × 100
What you need
Contribution margin is the share of revenue left after the costs that grow with each sale: cost of goods, shipping and, for online brands, marketing. You need these columns, here from the sample file monthly.csv:
- df["revenue"]: revenue (net of discounts)
- df["cogs"]: cost of goods sold
- df["shipping_cost"]: shipping cost
- df["marketing_spend"]: marketing spend
Step by step
- Install pandas (pip install pandas) and save monthly.csv next to your script.
- Run the script. The first print shows one row per month, the second the figure for the whole period.
- To use your own data, rename your columns to match, or change the names in the code.
The code
import pandas as pd
df = pd.read_csv("monthly.csv")
# Each month
df["contribution_margin"] = (df["revenue"] - df["cogs"] - df["shipping_cost"] - df["marketing_spend"]) / df["revenue"] * 100
print(df[["month", "contribution_margin"]].round(2))
# Whole period: add up the parts first, then divide
contribution_margin_total = (df["revenue"].sum() - df["cogs"].sum() - df["shipping_cost"].sum() - df["marketing_spend"].sum()) / df["revenue"].sum() * 100
print(round(contribution_margin_total, 2))
What you should get
Run against the sample data, the code above returns these values (in percent):
| Month | revenue | cogs | shipping_cost | marketing_spend | contribution_margin |
|---|---|---|---|---|---|
| 2026-01 | 4,12,000 | 2,26,600 | 49,100 | 96,000 | 9.78% |
| 2026-02 | 4,38,500 | 2,36,800 | 52,400 | 1,02,000 | 10.79% |
| 2026-03 | 4,71,200 | 2,59,200 | 56,800 | 1,18,000 | 7.89% |
| 2026-04 | 4,55,800 | 2,55,200 | 54,000 | 99,000 | 10.44% |
| 2026-05 | 5,02,300 | 2,71,200 | 59,200 | 1,24,000 | 9.54% |
| 2026-06 | 5,48,900 | 2,90,900 | 64,200 | 1,31,000 | 11.44% |
| Whole period | 28,28,700 | 15,39,900 | 3,35,700 | 6,70,000 | 10.01% |
(₹28,28,700 − ₹15,39,900 − ₹3,35,700 − ₹6,70,000) ÷ ₹28,28,700 × 100 = 10.01%
Notes for Python
- Dividing by zero in pandas does not raise an error: it gives inf or NaN. Check with df.replace([float("inf"), -float("inf")], float("nan")) before averaging or plotting.
- For the whole period, sum the columns first and then divide. df[col].mean() of the monthly results weights a small month the same as a large one.
- pct_change(), groupby() and resample() extend the same idea to weekly data, product categories and daily rows.
Where contribution margin misleads
- Definitions vary. Some include payment fees and packaging, some exclude marketing (often called CM1, CM2, CM3). State which costs you subtract.
- Marketing spent this month brings customers who buy next month too, so monthly contribution margin swings with campaign timing.
- Returns and undelivered COD orders carry shipping both ways. Include return shipping if it is material.
The full explanation, with a calculator, is on the Contribution margin page.
Same metric, other tools
Related metrics in Python (pandas)
Learn more about Python
- Python and SQL for data analysis, side by side — 3 min read
- How to find out what changed in your sales data — 2 min read
About the data on this page
The figures come from a small made-up dataset, printed above in full so you can check every number by hand. Download it as monthly.csv. The results are computed from those rows when the page is built, and the Excel formulas, SQL and Python on these pages are run against the same rows before publishing, so the code and the answers agree.
To get these numbers from your own data without writing formulas, try AnalyzeIt: ask in plain words and it shows the working behind every figure.
Legal entity
Devansh Yadav
Moradabad, Uttar Pradesh
Governing law: Courts at Moradabad, Uttar Pradesh
Phone: +91 8433475698
Legal: legal@analyzeit.in