Running Total of Revenue by Date
You're a data analyst at Whetcode Supply, an online shop selling desk gear. The finance lead is building the "money in, year to date" chart for the annual review, and the review wants one chart per category rather than one for the shop, so the category comes with the request. For every date the shop took an order for something in that category, the line shows everything that category earned from the first day of the year up to and including that day, so it never dips — it climbs or it stays flat. She wants the numbers behind the whole line, not just the figure it finishes on, because the flat stretches between busy weeks are the part the review argues about.
Three things are in the way. An order records what was bought and how many, never what it cost, so there is no money anywhere in the orders table — and nothing on an order row says which category it belongs to either. Two orders can share a date, and the chart has one point per date rather than one per order. And the line only means anything if the dates are read oldest first, which the table does not promise.
products_df — the catalogue. price is the cost of a single unit; category is how the buying team groups the range.
| id | name | category | price |
|---|---|---|---|
| 1 | Wireless Mouse | electronics | 25.0 |
| 2 | Standing Desk | furniture | 350.0 |
| 3 | Desk Lamp | furniture | 40.0 |
| 4 | Mechanical Keyboard | electronics | 85.0 |
| 5 | Notebook Set | office | 12.0 |
orders_df — one row per order placed this year. quantity is how many units of that product_id were bought.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
Input
Both DataFrames above are already built for you — the catalogue and order rows are not read from input. What does arrive is a single line naming the category the chart is being drawn for, spelled exactly as the category column spells it.
Task: Print a Python dict holding every point on that category's year-to-date line: keys are the order dates as they appear in the table, values are the running money-in figure on that date, rounded to 2 decimals. There is one entry per distinct date on which that category sold something, oldest date first. A category that has never sold anything has no line at all, so print an empty dict.
Example: a shop with two trading days might print {'2023-01-04': 90.5, '2023-01-09': 217.25}.
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in