AnalystPath

Daily Failed-Delivery Rate

PandasHardSenior level~10 min

Problem

A food-delivery platform logs every order, plus a flag marking whether each customer and each courier account is suspended.

DataFrame: `orders` (from `orders.csv`)

| Column | Type |
|-------------|--------|
| order_id | int |
| customer_id | int |
| courier_id | int |
| outcome | object |
| order_day | object |

`outcome` is one of `'delivered'`, `'failed_courier'`, `'failed_customer'`.

DataFrame: `accounts` (from `accounts.csv`)

| Column | Type |
|--------------|--------|
| account_id | int |
| suspended | object |
| account_kind | object |

`suspended` is `'yes'` or `'no'`; `account_kind` is `'customer'` or `'courier'`.

An order **counts** only if **both** its customer and its courier accounts are not suspended. The **failed rate** for a day is the number of counted orders that were not delivered divided by the total number of counted orders that day.

Write a function that reports, for each `order_day` from `'2024-02-01'` through `'2024-02-03'` that has at least one counted order, the day and its failed rate rounded to two decimals. Return columns `order_day` and `fail_rate`, in any order.

Input data

Example rows — the live problem includes the full dataset.

orders
order_idcustomer_idcourier_idoutcomeorder_day
1110delivered2024-02-01
2211failed_courier2024-02-01
3312delivered2024-02-01
4413failed_customer2024-02-01
5110delivered2024-02-02
accounts
account_idsuspendedaccount_kind
1nocustomer
2yescustomer
3nocustomer
4nocustomer
10nocourier

Expected output

Your answer should return 3 rows with the columns order_day, fail_rate.

Starter code (Pandas (Python))

import pandas as pd

def daily_fail_rate(orders: pd.DataFrame, accounts: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return orders

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