Monthly Shipments and Returns by Region
Problem
You are given two DataFrames. `shipments` (from `shipments.csv`) has columns `ship_id`, `region`, `status` (`'fulfilled'` or `'cancelled'`), `value_cents`, and `ship_date`. `returns` (from `returns.csv`) has columns `ship_id` and `return_date`; each row records that a shipment was returned on that date.
For each month and region, report the number of **fulfilled** shipments and their total value (count only `status == 'fulfilled'`), and the number of **returns** and their total value. A fulfilled shipment is counted in the month of its `ship_date`; a return is counted in the month of its `return_date`, using the region and value of the shipment it belongs to. Months use `YYYY-MM`. Output columns in order: `month`, `region`, `fulfilled_count`, `fulfilled_value`, `return_count`, `return_value`.
Input data
Example rows — the live problem includes the full dataset.
| ship_id | region | status | value_cents | ship_date |
|---|---|---|---|---|
| 101 | NW | fulfilled | 1000 | 2022-05-18 |
| 102 | NW | cancelled | 2000 | 2022-05-19 |
| 105 | NW | fulfilled | 5000 | 2022-06-15 |
| ship_id | return_date |
|---|---|
| 102 | 2022-05-29 |
| 101 | 2022-06-30 |
| 105 | 2022-09-18 |
Expected output
Your answer should return 3 rows with the columns month, region, fulfilled_count, fulfilled_value, return_count, return_value.
Starter code (Pandas (Python))
import pandas as pd
def monthly_flows(shipments, returns) -> pd.DataFrame:
# Your code here
return shipmentsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock