The question
Prepare a clear, exportable comparison of units sold by product category.
df Shareable figureYour inputs
df
One row is one recorded sale line. quantity is the units already sold, irrespective of current in_stock availability; unit_price is not additive.
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 20 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 |
| P019 | " pencil" | Writing | East | 3 | 0.8 | 0.5 | true |
| P020 | Note-book | Paper | West | 4 | 3.5 | 1 | true |
The editable setup in Your Python creates these inputs when you run it.
Prepare a clear, exportable comparison of units sold by product category.
unitsSeriesfigFigurechallenge.pngPNG fileDeliverables
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 unit totalsOutput
unitsSeries- Store the total
quantitysold for eachcategoryinunits.
- Store the total
- FigureOutput
figFigure- Create
figcomparingcategoryunit totals. - Choose labelled bars or individual points; either orientation is valid.
- Bars must start at zero.
- Add a meaningful title and measure labels
- Display the figure.
Identify Category and Units on the appropriate axes; extra wording is welcome.
- Create
- PNG exportOutput
challenge.pngPNG file- Save the finished
figaschallenge.pngat 150 dpi, including its labels.
- Save the finished
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
Which measure can be added meaningfully across order lines?
Hint 2 — Tools
groupby(), sum(), Axes.bar() or Axes.barh(), and Figure.savefig(), or Axes.scatter() for a labelled point comparison
Revisit: Summarise groups · Exact precomputed bars · Save a figure · Avoid misleading charts · Scatter plot
Hint 3 — Approach
Total units by category, create and label the comparison, then export the same finished figure.
Explained solution
Add these lines below # Your work in the editor. The supplied input setup is already there.
units = df.groupby('category').quantity.sum()
fig, ax = plt.subplots()
ax.bar(units.index, units.values)
ax.set(title='Units sold by category', xlabel='Category', ylabel='Units')
fig.tight_layout()
fig.savefig('challenge.png', dpi=150, bbox_inches='tight')
plt.show()View supplied setup
This code is already at the top of the editor.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Supplied input: df (20 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),
('P019', ' pencil', 'Writing', 'East', 3, 0.8, 0.5, True),
('P020', 'Note-book', 'Paper', 'West', 4, 3.5, 1, True),
],
columns=['order_id', 'product', 'category', 'region', 'quantity', 'unit_price', 'discount', 'in_stock'],
)Why these steps exist
- Units are additive across sale lines; neither the number of rows nor the sum of unit prices answers the question.
- Exporting after the figure is labelled ensures the saved evidence matches the finished view.
Another valid approach
Labelled individual points are also valid for comparing these summaries. Vertical and horizontal orientations are accepted; bars must use a zero baseline.