Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← All challenges
WC05 · Products / sales · ~10–20 min

Calculate a useful total

numeric quality · calculation · summaries

Sales totals

The question

Prepare usable sales lines and their monetary totals.

df Sales totals

Your inputs

df

quantity is units; unit_price is dollars per unit. All valid quantities and prices are nonnegative.

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.
df · first 5 of 16 rows
order_idproductcategoryregionquantityunit_pricediscountin_stock
P001" Notebook "PaperEast23.50false
P002PENWritingWest51.2Nonetrue
P003" pencil"WritingEast30.80.5true
P004Note-bookPaperWestbad3.51true
P005" Notebook "PaperEast13.50true
View all 16 rows
df · complete input
order_idproductcategoryregionquantityunit_pricediscountin_stock
P001" Notebook "PaperEast23.50false
P002PENWritingWest51.2Nonetrue
P003" pencil"WritingEast30.80.5true
P004Note-bookPaperWestbad3.51true
P005" Notebook "PaperEast13.50true
P006PENWritingWest61.2Nonefalse
P007" pencil"WritingEast20.80.5true
P008Note-bookPaperWest8None1true
P009" Notebook "PaperEast23.50true
P010PENWritingWest51.2Nonetrue
P011" pencil"WritingEast30.80.5false
P012Note-bookPaperWest43.51true
P013" Notebook "PaperEast13.50true
P014PENWritingWest61.2Nonetrue
P015" pencil"WritingEast20.80.5true
P016Note-bookPaperWest83.51false

The editable setup in Your Python creates these inputs when you run it.

Your task · Calculate a useful total

Prepare usable sales lines and their monetary totals.

cleanDataFrametotal_salesnumber

Deliverables

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
eligible
rows that meet this task’s stated conditions
  1. Priced order linesOutputcleanDataFrame
    1. Create clean by parsing quantity and unit_price numerically, excluding rows where either is unknown.
    2. Add line_total = quantity × unit_price.
    3. Keep other columns and source order.
  2. Total salesOutputtotal_salesnumber
    1. Store the sum of eligible line_total values in total_sales.

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 quantities are additive money amounts, and which are unit prices?

Hint 2 — Tools

to_numeric(), dropna(), arithmetic and sum()

Revisit: Convert numeric text · Drop missing rows · Create a numeric column

Hint 3 — Approach

Parse the two required measures, retain complete lines, calculate each line amount and total those amounts.

Explained solution

Add these lines below # Your work in the editor. The supplied input setup is already there.

clean = df.copy()
clean['quantity'] = pd.to_numeric(clean.quantity, errors='coerce')
clean['unit_price'] = pd.to_numeric(clean.unit_price, errors='coerce')
clean = clean.dropna(subset=['quantity', 'unit_price']).copy()
clean['line_total'] = clean.quantity * clean.unit_price
total_sales = clean.line_total.sum()
clean
View 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', '0.8', 0.5, True),
        ('P004', 'Note-book', 'Paper', 'West', 'bad', '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', None, 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),
    ],
    columns=['order_id', 'product', 'category', 'region', 'quantity', 'unit_price', 'discount', 'in_stock'],
)

Why these steps exist

  • Quantity and unit price must both be numeric before multiplication is meaningful.
  • Line amounts are additive dollars; summing unit prices would not measure sales.

Another valid approach

Direct column arithmetic and assign-based construction both produce the same line totals.

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Named outputs appear below when you run.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.