잘못된 값 탐지

PostgreSQL 데이터베이스에서 데이터 정제하기

Darryl Reeves, Ph.D.

Assistant Professor, Long Island University - Brooklyn

잘못된 데이터 값

  camis   |              name               | inspection_date | score |            inspection_type            | ... 
 ---------+---------------------------------+-----------------+-------+---------------------------------------+-----
 ...      | ...                             | ...             | ...   | ...                                   | ...
 41659848 | LA BRISA DEL CIBAO              | 01/30/2018      | 20    | Cycle Inspection / Initial Inspection | ...
 40961447 | MESON SEVILLA RESTAURANT        | 03/19/2019      | 50    | Cycle Inspection / Initial Inspection | ...
 50063071 | WA BAR                          | 05/23/2018      | 15    | Cycle Inspection / Initial Inspection | ...
 50034992 | EMPANADAS MONUMENTAL            | 06/21/2019      | 17    | Cycle Inspection / Re-inspection      | ...
 50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020      | 10    | Cycle Inspection / Initial Inspection | ...
 ...      | ...                             | ...             | ...   | ...                                   | ...
  camis   |             name             | inspection_date | score |                 inspection_type                 | ... 
 ---------+------------------------------+-----------------+-------+-------------------------------------------------+-----
 ...      | ...                          | ...             | ...   | ...                                             | ...
 41104041 | THE SPARROW TAVERN           | 09/17/2019      | 13    | Cycle Inspection / Initial Inspection           | ...
 50016937 | BURGER KING                  | 09/14/2018      | 12    | Cycle Inspection / Re-inspection                | ...
 50066469 | DARBAR'S CHICKEN & RIBS      | 08/07/2017      | 11    | Pre-permit (Operational) / Reopening Inspection | ...
 41195691 | F & J PINE RESTAURANT        | 05/02/2019      | 26    | Cycle Inspection / Initial Inspection           | ...
 50015706 | EL RINCONCITO DE LOS SABORES | 12/18/2019      | A     | Cycle Inspection / Initial Inspection           | ...
 ...      | ...                          | ...             | ...   | ...                                             | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

패턴 매칭으로 잘못된 데이터 처리

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score NOT SIMILAR TO '\d+';
PostgreSQL 데이터베이스에서 데이터 정제하기

패턴 매칭으로 잘못된 데이터 처리

  • 쿼리는 숫자가 아닌 문자만 제한합니다
  • 값의 길이는 제한하지 않습니다
SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score NOT SIMILAR TO '\d{1}' AND
  score NOT SIMILAR TO '\d{2}' AND
  score NOT SIMILAR TO '\d{3}';
PostgreSQL 데이터베이스에서 데이터 정제하기

타입 제약 사용하기

  • 열에는 정수가 담겨 있습니다
  • 열은 비정수를 허용하지 않아야 합니다
ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;
  • SMALLINT: -32,768 ~ 32,767
  • USING 절은 이전 값의 변환을 지정합니다
PostgreSQL 데이터베이스에서 데이터 정제하기

정규식 기초 복습

Metacharacter Usage Example RE Example Match
\d 숫자(0-9)와 일치 \d\d\d '345'
? 이전 문자의 0개 또는 1개와 일치 x\d? 'x5'
+ 이전 문자의 1개 이상과 일치 \d+ '10'
* 임의 문자 0회 이상과 일치 \d* '3081'
[] 대괄호 안의 문자와 일치 [a-z] 'f'
PostgreSQL 데이터베이스에서 데이터 정제하기

타입 제약으로 범위 제약 설정

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0;
PostgreSQL 데이터베이스에서 데이터 정제하기

타입 제약으로 범위 제약 설정

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score <= -1;
PostgreSQL 데이터베이스에서 데이터 정제하기

타입 제약으로 범위 제약 설정

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0 OR
  score > 100;
PostgreSQL 데이터베이스에서 데이터 정제하기

타입 제약으로 범위 제약 설정

ALTER TABLE restaurant_inspection
ALTER COLUMN score TYPE SMALLINT USING score::smallint;

SELECT
  camis,
  name,
  inspection_date,
  score
FROM
  restaurant_inspection
WHERE
  score < 0 OR
  score >= 101;
PostgreSQL 데이터베이스에서 데이터 정제하기

BETWEEN 연산자

SELECT
  camis, name, inspection_date, score
FROM
  restaurant_inspection
WHERE
  score NOT BETWEEN 0 AND 100;
  camis   |          name           | inspection_date | score 
 ---------+-------------------------+-----------------+-------
 ...      | ...                     | ...             |   ...
 41702543 | TROPICAL GRILL          | 05/14/2018      |   109
 50074058 | PAD THAI                | 08/01/2018      |   101
 50085349 | DON CHILE MEXICAN GRILL | 12/04/2018      |   124
 50092932 | ENERGY JUICE BAR        | 06/24/2019      |   102
 41702543 | TROPICAL GRILL          | 05/14/2018      |   109
 50034653 | KAI FAN ASIAN CUISINE   | 12/06/2019      |    -1
 ...      | ...                     | ...             |   ...
PostgreSQL 데이터베이스에서 데이터 정제하기

연습해 봅시다!

PostgreSQL 데이터베이스에서 데이터 정제하기

Preparing Video For Download...