데이터 정제 소개

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

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

왜 데이터 정제가 중요한가요?

  • 데이터는 지저분합니다
  • 분석 전 정제가 자주 필요합니다
  • 열 타입 제약을 활용하면 유용합니다
  • 이 강의는 방어적 접근이 불가/부적용일 때에 초점을 둡니다
PostgreSQL 데이터베이스에서 데이터 정제하기

문자열 데이터 정제

  • 문자열 데이터: 많고 유연하며 자주 지저분함
 name                            | grade |            inspection_type              | census_tract | ... 
 --------------------------------+-------+-----------------------------------------+--------------+-----
 ...                             | ...   | ...                                     | ...          | ...
 EMPANADAS MONUMENTAL            | B     | Cycle Inspection  /  Re-inspection      | 26900        | ...
 ALPHONSO'S PIZZERIA & TRATTORIA | A     | Cycle Inspection  /  Initial Inspection | 202          | ...
 THE SPARROW TAVERN              | A     | Cycle Inspection  /  Initial Inspection | 12500        | ...
 BURGER KING                     | A     | Cycle Inspection  /  Re-inspection      | 86400        | ...
 ASTORIA PIZZA                   | B     | Cycle Inspection  /  Re-inspection      | 6300         | ...
 ...                             | ...   | ...                                     | ...          | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

문자열 데이터 정제

 name                            | grade |            inspection_type              | census_tract | ... 
 --------------------------------+-------+-----------------------------------------+--------------+-----
 ...                             | ...   | ...                                     | ...          | ...
 EMPANADAS MONUMENTAL            | B     | Cycle Inspection  /  Re-inspection      | 26900        | ...
 ALPHONSO'S PIZZERIA & TRATTORIA | A     | Cycle Inspection  /  Initial Inspection | 202          | ...
 THE SPARROW TAVERN              | A     | Cycle Inspection  /  Initial Inspection | 12500        | ...
 BURGER KING                     | A     | Cycle Inspection  /  Re-inspection      | 86400        | ...
 ASTORIA PIZZA                   | B     | Cycle Inspection  /  Re-inspection      | 6300         | ...
 ...                             | ...   | ...                                     | ...          | ...
  1. name의 대소문자 제한
  2. inspection_type의 구분 공백 제거
  3. census_tract 길이 통일
PostgreSQL 데이터베이스에서 데이터 정제하기

문자열 데이터 정제

 name                            | grade |            inspection_type              | census_tract | ... 
 --------------------------------+-------+-----------------------------------------+--------------+-----
 ...                             | ...   | ...                                     | ...          | ...
 EMPANADAS MONUMENTAL            | B     | Cycle Inspection  /  Re-inspection      | 26900        | ...
 ALPHONSO'S PIZZERIA & TRATTORIA | A     | Cycle Inspection  /  Initial Inspection | 202          | ...
 THE SPARROW TAVERN              | A     | Cycle Inspection  /  Initial Inspection | 12500        | ...
 BURGER KING                     | A     | Cycle Inspection  /  Re-inspection      | 86400        | ...
 ASTORIA PIZZA                   | B     | Cycle Inspection  /  Re-inspection      | 6300         | ...
 ...                             | ...   | ...                                     | ...          | ...
 name                            | grade |           inspection_type             | census_tract | ... 
 --------------------------------+-------+---------------------------------------+--------------+-----
 ...                             | ...   | ...                                   | ...          | ...
 Empanadas Monumental            | B     | Cycle Inspection / Re-inspection      | 026900       | ...
 Alphonso'S Pizzeria & Trattoria | A     | Cycle Inspection / Initial Inspection | 000202       | ...
 The Sparrow Tavern              | A     | Cycle Inspection / Initial Inspection | 012500       | ...
 Burger King                     | A     | Cycle Inspection / Re-inspection      | 086400       | ...
 Astoria Pizza                   | B     | Cycle Inspection / Re-inspection      | 006300       | ...
 ...                             | ...   | ...                                   | ...          | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

INITCAP() 함수 사용

INITCAP(input_string) - 대소문자 정리

SELECT INITCAP('HELLO FRIEND!');
Hello Friend!
PostgreSQL 데이터베이스에서 데이터 정제하기

REPLACE() 함수 사용

REPLACE(input_string, to_replace, replacement) - 한 텍스트를 다른 텍스트로 대체

SELECT REPLACE('180 Main Street', 'Street', 'St');
180 Main St
PostgreSQL 데이터베이스에서 데이터 정제하기

LPAD() 함수 사용

LPAD(input_string, length [, fill_value]) - 문자열 앞에 값 추가

SELECT LPAD('123', 7, 'X');
XXXX123
PostgreSQL 데이터베이스에서 데이터 정제하기

문자열 정제 쿼리 만들기

SELECT
  INITCAP(name) as name,
  grade,
  REPLACE(inspection_type, '  /  ', ' / ') as inspection_type, 
  LPAD(census_tract, 6, '0') as census_tract
FROM
  restaurant_inspection;
 name                            | grade |           inspection_type             | census_tract | ... 
 --------------------------------+-------+---------------------------------------+--------------+-----
 ...                             | ...   | ...                                   | ...          | ...
 Empanadas Monumental            | B     | Cycle Inspection / Re-inspection      | 026900       | ...
 Alphonso'S Pizzeria & Trattoria | A     | Cycle Inspection / Initial Inspection | 000202       | ...
 The Sparrow Tavern              | A     | Cycle Inspection / Initial Inspection | 012500       | ...
 Burger King                     | A     | Cycle Inspection / Re-inspection      | 086400       | ...
 Astoria Pizza                   | B     | Cycle Inspection / Re-inspection      | 006300       | ...
 ...                             | ...   | ...                                   | ...          | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

연습해 봅시다!

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

Preparing Video For Download...