The question
Compare product categories across regions using available stock only.
df Category matrixYour inputs
df
One row is an order line. Count lines regardless of their quantity.
Field meanings and units
order_id- Order or order-line identifier, as defined by this brief.
product- Recorded product label.
category- Product category.
region- Sales region.
quantity- Number of units.
unit_price- Dollars per unit.
discount- Discount in dollars for the whole order line.
in_stock- Whether the order line is marked available.
order_id | product | category | region | quantity | unit_price | discount | in_stock |
|---|---|---|---|---|---|---|---|
| P001 | " Notebook " | Paper | East | 2 | 3.5 | 0 | false |
| P002 | PEN | Writing | West | 5 | 1.2 | None | true |
| P003 | " pencil" | Writing | East | 3 | 0.8 | 0.5 | true |
| P004 | Note-book | Paper | West | 4 | 3.5 | 1 | true |
| P005 | " Notebook " | Paper | East | 1 | 3.5 | 0 | true |
View all 18 rows
order_id | product | category | region | quantity | unit_price | discount | in_stock |
|---|---|---|---|---|---|---|---|
| P001 | " Notebook " | Paper | East | 2 | 3.5 | 0 | false |
| P002 | PEN | Writing | West | 5 | 1.2 | None | true |
| P003 | " pencil" | Writing | East | 3 | 0.8 | 0.5 | true |
| P004 | Note-book | Paper | West | 4 | 3.5 | 1 | true |
| P005 | " Notebook " | Paper | East | 1 | 3.5 | 0 | true |
| P006 | PEN | Writing | West | 6 | 1.2 | None | false |
| P007 | " pencil" | Writing | East | 2 | 0.8 | 0.5 | true |
| P008 | Note-book | Paper | West | 8 | 3.5 | 1 | true |
| P009 | " Notebook " | Paper | East | 2 | 3.5 | 0 | true |
| P010 | PEN | Writing | West | 5 | 1.2 | None | true |
| P011 | " pencil" | Writing | East | 3 | 0.8 | 0.5 | false |
| P012 | Note-book | Paper | West | 4 | 3.5 | 1 | true |
| P013 | " Notebook " | Paper | East | 1 | 3.5 | 0 | true |
| P014 | PEN | Writing | West | 6 | 1.2 | None | true |
| P015 | " pencil" | Writing | East | 2 | 0.8 | 0.5 | true |
| P016 | Note-book | Paper | West | 8 | 3.5 | 1 | false |
| P017 | " Notebook " | Paper | East | 2 | 3.5 | 0 | true |
| P018 | PEN | Writing | West | 5 | 1.2 | None | true |
The editable setup in Your Python creates these inputs when you run it.
Compare product categories across regions using available stock only.
cross_tabDataFramecategoriesnumberDeliverables
Make the outputs listed below. Use each name exactly as shown. Then choose Check answer.
Python name key
Teal outlines name table columns. Violet left rules name Python objects. Dotted rules mark syntax, functions and parameters. Use each name exactly as shown.
- Category by region countsOutput
cross_tabDataFrame- Create
cross_tabwith one row percategoryand one column perregionforavailable(in_stock=True) records. - Each cell is the number of records in that
categoryandregion, not the sum ofquantity.
- Create
- Observed categoriesOutput
categoriesnumber- Store the number of distinct
categoriesin thein_stock=True rows incategories.
- Store the number of distinct
Data policy Keep all supplied source tables unchanged. Work on separate outputs.
Help Hints & explained solution
Choose the amount of help you need. Opening one hint does not reveal the others.
Hint 1 — Think
Is the requested cell a count of records or a sum of quantities?
Hint 2 — Tools
Boolean selection, pd.crosstab() for category-by-region counts, and nunique()
Revisit: Meet a DataFrame · Summarise groups · What values exist? · Combine conditions · Answer one numerical question
Hint 3 — Approach
Select available records, count category and region combinations, then count distinct available categories.
Explained solution
Add these lines below # Your work in the editor. The supplied input setup is already there.
available = df[df.in_stock]
cross_tab = pd.crosstab(available['category'], available['region'])
categories = available['category'].nunique()
cross_tabView supplied setup
This code is already at the top of the editor.
import pandas as pd
# Supplied input: df (18 rows)
df = pd.DataFrame(
[
('P001', ' Notebook ', 'Paper', 'East', 2, 3.5, 0, False),
('P002', 'PEN', 'Writing', 'West', 5, 1.2, None, True),
('P003', ' pencil', 'Writing', 'East', 3, 0.8, 0.5, True),
('P004', 'Note-book', 'Paper', 'West', 4, 3.5, 1, True),
('P005', ' Notebook ', 'Paper', 'East', 1, 3.5, 0, True),
('P006', 'PEN', 'Writing', 'West', 6, 1.2, None, False),
('P007', ' pencil', 'Writing', 'East', 2, 0.8, 0.5, True),
('P008', 'Note-book', 'Paper', 'West', 8, 3.5, 1, True),
('P009', ' Notebook ', 'Paper', 'East', 2, 3.5, 0, True),
('P010', 'PEN', 'Writing', 'West', 5, 1.2, None, True),
('P011', ' pencil', 'Writing', 'East', 3, 0.8, 0.5, False),
('P012', 'Note-book', 'Paper', 'West', 4, 3.5, 1, True),
('P013', ' Notebook ', 'Paper', 'East', 1, 3.5, 0, True),
('P014', 'PEN', 'Writing', 'West', 6, 1.2, None, True),
('P015', ' pencil', 'Writing', 'East', 2, 0.8, 0.5, True),
('P016', 'Note-book', 'Paper', 'West', 8, 3.5, 1, False),
('P017', ' Notebook ', 'Paper', 'East', 2, 3.5, 0, True),
('P018', 'PEN', 'Writing', 'West', 5, 1.2, None, True),
],
columns=['order_id', 'product', 'category', 'region', 'quantity', 'unit_price', 'discount', 'in_stock'],
)Why these steps exist
- The cells count order lines, not quantities, so large orders do not weigh more than small ones.
- The category and region labels identify each count; their alphabetical order does not affect the result.
Another valid approach
Grouping by category and region and then reshaping the counts is also valid. Keep the category and region labels attached to their values.