🎓 A1Learner
🔍
0 of 50 lessons completed

Project: Join the Products Table

The products table holds the cost of each product. Merging it with sales gives the profit of every order.

Join the cost table

The merge adds a cost column to each order. The first three orders show the join.

import io
import pandas as pd

SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

PRODUCTS_CSV = """
product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Lamp,Home,15.00"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
merged = sales.merge(products[["product", "cost"]], on="product")
print(merged.head(3))

Profit per order

Price minus cost is the profit on one item. Times units is the profit on the order.

import io
import pandas as pd

SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

PRODUCTS_CSV = """
product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Lamp,Home,15.00"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
merged = sales.merge(products[["product", "cost"]], on="product")
merged["profit"] = (merged["price"] - merged["cost"]) * merged["units"]
print(merged["profit"].sum())

Profit by category

groupby adds the profit up for each category. Home earns the most.

import io
import pandas as pd

SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

PRODUCTS_CSV = """
product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Lamp,Home,15.00"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
merged = sales.merge(products[["product", "cost"]], on="product")
merged["profit"] = (merged["price"] - merged["cost"]) * merged["units"]
print(merged.groupby("category")["profit"].sum())

Example

import io
import pandas as pd

SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

PRODUCTS_CSV = """
product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Lamp,Home,15.00"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
merged = sales.merge(products[["product", "cost"]], on="product")
merged["profit"] = (merged["price"] - merged["cost"]) * merged["units"]
answer = merged.groupby("category")["profit"].sum().sort_values(ascending=False)
print(answer)
print(merged.shape)

Key points

  • Use merge to join sales to the products table.
  • The shared column here is product.
  • Pick the needed columns before the merge to keep the result tidy.
  • Profit is price minus cost, times units.
  • A merge keeps only rows that match on both sides.

In this example, both tables load from their CSV text. The merge adds the cost of each product to its orders. Profit is price minus cost, times units. groupby adds the profit up by category, and sort_values puts Home first. shape confirms twelve rows and nine columns.

Note

Both tables hold a category column, so the merge would add _x and _y to the names. Picking cost alone keeps the result tidy.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which column joins sales to the products table?