본문 바로가기
Data Analysis/Query

리트코드 : 1527. Patients With a Condition

by 베짱이28호 2024. 8. 31.

리트코드 : 1527. Patients With a Condition


문제

SQL Schema
Pandas Schema
Table: Patients

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| patient_id   | int     |
| patient_name | varchar |
| conditions   | varchar |
+--------------+---------+
patient_id is the primary key (column with unique values) for this table.
'conditions' contains 0 or more code separated by spaces. 
This table contains information of the patients in the hospital.


Write a solution to find the patient_id, patient_name, and conditions of the patients who have Type I Diabetes. Type I Diabetes always starts with DIAB1 prefix.

Return the result table in any order.

The result format is in the following example.



Example 1:

Input: 
Patients table:
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 1          | Daniel       | YFEV COUGH   |
| 2          | Alice        |              |
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 |
| 5          | Alain        | DIAB201      |
+------------+--------------+--------------+
Output: 
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 | 
+------------+--------------+--------------+
Explanation: Bob and George both have a condition that starts with DIAB1.
  • DAIB1로 시작하는 conditions 찾기.

문제 풀이

MySQL

SELECT *
FROM patients
WHERE conditions REGEXP '\\bDIAB1'
  • 문자열을 보고 like로 접근할 수 도 있는데, DIAB1 앞에 다른 접두어가 붙어있는 경우가 있다.
  • 이러한 경우에는 단독 매칭을 시켜주는 boundary를 사용해야 한다고 한다.

Pandas

import pandas as pd

def find_patients(patients: pd.DataFrame) -> pd.DataFrame:
    return patients[patients['conditions'].str.contains('\\bDIAB1')]
    # return patients[patients['conditions'].str.contains(r'\bDIAB1')]
  • 마찬가지로 boundary 지정 \b를 통해서 풀이.
  • 파이썬에서는 raw string을 지정해주는 r이 있어서 \을 사용하지 않고 r이랑 같이 사용할 수 있다.
  • raw string 지정하면, 내부 타입에서 바뀌는게 있어서 그런가 시간이 더 느리게 나왔다

코멘트

  • 정규표현식 날 잡고 한 번 정리 해야할듯....

댓글