top of page

Customers Who Never Order

LeetCode Problem 183

Customers Who Never Order
Write a solution to find all customers who never order anything. Return the result table in any order.

import pandas as pd

data = [[1, 'Joe'], [2, 'Henry'], [3, 'Sam'], [4, 'Max']]
customers = pd.DataFrame(data, columns=['id', 'name']).astype({'id':'Int64', 'name':'object'})
data = [[1, 3], [2, 1]]
orders = pd.DataFrame(data, columns=['id', 'customerId']).astype({'id':'Int64', 'customerId':'Int64'})

def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
    df = customers.merge(orders, left_on='id', right_on='customerId', how='left')
    df = df[df['customerId'].isna()]
    df = df.rename(columns={"name":"Customers"})
    return df[["Customers"]]

result = find_customers(customers, orders)
print(result)

bottom of page