코딩 테스트/Pandas

Replace Employee ID With The Unique Identifier

susinlee 2024. 12. 19. 09:18

[문제]

Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.

Return the result table in any order.

The result format is in the following example.

 

 

[풀이]

1. unique_id 가 존재하지 않으면 null을 표시해야하니까 employee 테이블 기준으로 LEFT 조인을 해야함

2. unique_id와 name을 조회

 

Pandas

import pandas as pd

def replace_employee_id(employees: pd.DataFrame, employee_uni: pd.DataFrame) -> pd.DataFrame:
    # df = employees.merge(employee_uni, on='id', how='left')
    df = pd.merge(employees, employee_uni, on='id', how='left')
    
    return df[['unique_id', 'name']]

 

SQL

SELECT unique_id,
       name
FROM Employees E
LEFT JOIN EmployeeUNI U
ON E.id = U.id

'코딩 테스트 > Pandas' 카테고리의 다른 글

Rising Temperature  (0) 2024.12.21
Customer Who Visited but Did Not Make Any Transcations  (1) 2024.12.20
Product Sales Analysis 1  (0) 2024.12.20
대충 만든 자판  (0) 2024.12.19
Invalid Tweets  (0) 2024.12.19