การทำความสะอาดข้อมูลเบื้องต้น

การทำความสะอาดข้อมูลในฐานข้อมูล PostgreSQL

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

ทำไมการทำความสะอาดข้อมูลจึงสำคัญ?

  • ข้อมูลมักมีความซับซ้อนและไม่เป็นระเบียบ
  • ก่อนวิเคราะห์ มักต้องผ่านการทำความสะอาดก่อน
  • การกำหนดประเภทคอลัมน์ช่วยจัดการปัญหาได้
  • คอร์สนี้เน้นกรณีที่ไม่สามารถใช้แนวทางป้องกันล่วงหน้าได้
การทำความสะอาดข้อมูลในฐานข้อมูล PostgreSQL

การทำความสะอาดข้อมูล String

  • ข้อมูล String: มีมาก ยืดหยุ่น และมักไม่เป็นระเบียบ
 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

การทำความสะอาดข้อมูล String

 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

การทำความสะอาดข้อมูล String

 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]) - เติมข้อความไว้ด้านหน้า String

SELECT LPAD('123', 7, 'X');
XXXX123
การทำความสะอาดข้อมูลในฐานข้อมูล PostgreSQL

สร้างคิวรีสำหรับทำความสะอาดข้อมูล String

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...