Customers Who Never Order — LeetCode 183 Python Solution
EasyDatabase
- Problem
- #183
- Reading time
- 2 min
- Source
- leetcode.com
The problem
List the customers who have never placed a single order. Customers has id (int, primary key) and name (varchar); Orders has id (int, primary key) and customerId (int), which references a customer. Return the names in a column called Customers.
Example
- Input
- Customers(id, name) = (1, 'Ana'), (2, 'Ben'), (3, 'Cleo'), (4, 'Dev'); Orders(id, customerId) = (1, 3), (2, 1)
- Output
- Customers = 'Ben', 'Dev'
- Explanation
- Orders exist only for customers 1 and 3, so Ben and Dev never ordered.
Python solution
Python
import pandas as pd
def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
# Select the customers whose 'id' is not present in the orders DataFrame's 'customerId' column.
df = customers[~customers["id"].isin(orders["customerId"])]
# Build a DataFrame that only contains the 'name' column and rename it as 'Customers'.
df = df[["name"]].rename(columns={"name": "Customers"})
return dfComplexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 183. Customers Who Never Order?
- LeetCode 183. Customers Who Never Order is rated Easy on LeetCode.
- What topics does LeetCode 183. Customers Who Never Order cover?
- LeetCode 183. Customers Who Never Order is tagged Database on LeetCode.