The question
Prepare net order amounts under the billing team’s explicit missing-value policy.
df Net amountsYour inputs
df
discount is dollars per entire order line, not per unit. Unknown unit prices cannot be assumed zero.
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 | None | 0.5 | true |
| P004 | Note-book | Paper | West | 4 | 3.5 | 1 | true |
| P005 | " Notebook " | Paper | East | 1 | 3.5 | 0 | true |
View all 16 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 | None | 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 | None | true |
| P008 | Note-book | Paper | West | 8 | 3.5 | 1 | true |
| P009 | " Notebook " | Paper | East | None | 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 |
The editable setup in Your Python creates these inputs when you run it.
Prepare net order amounts under the billing team’s explicit missing-value policy.
cleanDataFrameexcluded_countnumberDeliverables
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.
Words in this task
- missing
- no value was supplied
- Net order linesOutput
cleanDataFrame- Create
cleankeeping knownunit_priceandquantity. - A missing
discountmeans nodiscount: fill it with zero. - Add
net_amount=quantity×unit_price−discount. - Keep other fields and source order.
- Create
- Unpriceable recordsOutput
excluded_countnumber- Store the number of excluded source rows in
excluded_count.
- Store the number of excluded source rows in
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
Does the same missing-value policy make sense for both a price and a discount?
Hint 2 — Tools
dropna(), fillna(), copy() and arithmetic
Revisit: Drop missing rows · Fill missing values · Create a numeric column
Hint 3 — Approach
Remove unpriceable lines, apply the authorised discount policy and compute net amounts.
Explained solution
Add these lines below # Your work in the editor. The supplied input setup is already there.
clean = df.dropna(subset=['unit_price', 'quantity']).copy()
clean['discount'] = clean.discount.fillna(0)
clean['net_amount'] = clean.quantity * clean.unit_price - clean.discount
excluded_count = len(df) - len(clean)
cleanView supplied setup
This code is already at the top of the editor.
import pandas as pd
# Supplied input: df (16 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, None, 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, None, True),
('P008', 'Note-book', 'Paper', 'West', 8, 3.5, 1, True),
('P009', ' Notebook ', 'Paper', 'East', None, 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),
],
columns=['order_id', 'product', 'category', 'region', 'quantity', 'unit_price', 'discount', 'in_stock'],
)Why these steps exist
- A missing discount has an authorised zero meaning. A missing quantity or price has no such meaning.
- The discount applies once to the whole line, after quantity times unit price.
Another valid approach
You may fill discounts before or after selecting eligible rows, provided no unknown price is replaced.