AnalystPath

Monthly Shipments and Returns by Region

PandasMediumMid level~10 min

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.

shipments
ship_idregionstatusvalue_centsship_date
101NWfulfilled10002022-05-18
102NWcancelled20002022-05-19
105NWfulfilled50002022-06-15
returns
ship_idreturn_date
1022022-05-29
1012022-06-30
1052022-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 shipments

Solve 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

Related Pandas questions