Giới thiệu về làm sạch dữ liệu

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Vì sao làm sạch dữ liệu quan trọng?

  • Dữ liệu thường lộn xộn
  • Trước khi phân tích, thường cần làm sạch
  • Hữu ích khi dùng ràng buộc kiểu cột
  • Khóa học tập trung khi không thể/dùng được cách tiếp cận phòng ngừa
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Làm sạch dữ liệu chuỗi

  • Dữ liệu chuỗi: nhiều, linh hoạt, và hay lộn xộn
 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         | ...
 ...                             | ...   | ...                                     | ...          | ...
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Làm sạch dữ liệu chuỗi

 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. Chuẩn hóa viết hoa trong name
  2. Xóa khoảng trắng thừa ở inspection_type
  3. Làm census_tract có độ dài đồng nhất
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Làm sạch dữ liệu chuỗi

 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       | ...
 ...                             | ...   | ...                                   | ...          | ...
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Sử dụng hàm INITCAP()

INITCAP(input_string) - sửa viết hoa

SELECT INITCAP('HELLO FRIEND!');
Hello Friend!
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Sử dụng hàm REPLACE()

REPLACE(input_string, to_replace, replacement) - thay một giá trị văn bản bằng giá trị khác

SELECT REPLACE('180 Main Street', 'Street', 'St');
180 Main St
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Sử dụng hàm LPAD()

LPAD(input_string, length [, fill_value]) - thêm tiền tố vào chuỗi

SELECT LPAD('123', 7, 'X');
XXXX123
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Xây dựng truy vấn làm sạch chuỗi

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       | ...
 ...                             | ...   | ...                                   | ...          | ...
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Ayo berlatih!

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Preparing Video For Download...