584. Find Customer Referee
Description
Pandas Schema
data = [[1, 'Will', None], [2, 'Jane', None], [3, 'Alex', 2], [4, 'Bill', None], [5, 'Zack', 1], [6, 'Mark', 2]]
customer = pd.DataFrame(data, columns=['id', 'name', 'referee_id']).astype({'id':'Int64', 'name':'object', 'referee_id':'Int64'})
Table: Customer
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | referee_id | int | +-------------+---------+ In SQL, id is the primary key column for this table. Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
Find the names of the customer that are either:
- referred by any customer with
id != 2
. - not referred by any customer.
- Return the result table in any order.
The result format is in the following example.
Example 1:
- Input:
Customer table: +----+------+------------+ | id | name | referee_id | +----+------+------------+ | 1 | Will | null | | 2 | Jane | null | | 3 | Alex | 2 | | 4 | Bill | null | | 5 | Zack | 1 | | 6 | Mark | 2 | +----+------+------------+
- Output:
+------+ | name | +------+ | Will | | Jane | | Bill | | Zack | +------+
💡 Hint 1:
Be careful of the NULL value
Submitted Code
import pandas as pd
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
# NaN 값은 0으로 변환
return customer[customer["referee_id"].fillna(0) != 2][["name"]]
Runtime: 274 ms | Beats 83.58%
Memory: 66.79 MB | Beats 92.63%
referee_id 열의 값이 2가 아닌 경우에 모두 해당하기 때문에 NaN 값을 0으로 변경했다.
Other Solutions
1st
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
return customer[(customer['referee_id']!=2) | (customer['referee_id'].isna())][['name']]
|
연산자로 두 조건을 필터링했다.
2nd
select name from customer
where referee_id != 2 or referee_id is null;
MySQL 버전이다.