183. 从不订购的客户

发布时间 2023-08-12 21:07:25作者: 吾执青剑向天涯

183. 从不订购的客户

2023年8月12日20:15:58

183. 从不订购的客户

简单

SQL Schema


Pandas Schema


Customers 表:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+
在 SQL 中,id 是该表的主键。
该表的每一行都表示客户的 ID 和名称。

Orders 表:

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| customerId  | int  |
+-------------+------+
在 SQL 中,id 是该表的主键。
customerId 是 Customers 表中 ID 的外键( Pandas 中的连接键)。
该表的每一行都表示订单的 ID 和订购该订单的客户的 ID。

找出所有从不点任何东西的顾客。

任意顺序 返回结果表。

结果格式如下所示。

示例 1:

输入:
Customers table:
+----+-------+
| id | name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
输出:
+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

通过次数

345.7K

提交次数

521.9K

通过率

66.2%

答案

import pandas as pd

def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
   return customers[~customers["id"].isin(orders["customerId"].values.tolist())].rename(columns={'name':'Customers'})[["Customers"]]




#  合并写法
    # customers = pd.merge(customers,orders,how='left', left_on='id',right_on='customerId').rename(columns={'name':'Customers'})
    # return customers[['Customers']][customers["customerId"].isnull()]