Introducere în curățarea datelor

Curățarea datelor în baze de date PostgreSQL

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

De ce este importantă curățarea datelor?

  • Datele sunt adesea dezordonate
  • Înainte de analiză, necesită de regulă curățare
  • Util să se folosească constrângeri de tip pentru coloane
  • Cursul se concentrează pe cazurile în care abordările preventive nu sunt disponibile/aplicabile
Curățarea datelor în baze de date PostgreSQL

Curățarea datelor de tip șir

  • Date de tip șir: abundente, flexibile și adesea dezordonate
 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         | ...
 ...                             | ...   | ...                                     | ...          | ...
Curățarea datelor în baze de date PostgreSQL

Curățarea datelor de tip șir

 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. Standardizarea majusculelor în name
  2. Eliminarea spațiilor suplimentare din separatorul din inspection_type
  3. Uniformizarea lungimii valorilor din census_tract
Curățarea datelor în baze de date PostgreSQL

Curățarea datelor de tip șir

 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       | ...
 ...                             | ...   | ...                                   | ...          | ...
Curățarea datelor în baze de date PostgreSQL

Utilizarea funcției INITCAP()

INITCAP(input_string) - corectarea majusculelor

SELECT INITCAP('HELLO FRIEND!');
Hello Friend!
Curățarea datelor în baze de date PostgreSQL

Utilizarea funcției REPLACE()

REPLACE(input_string, to_replace, replacement) - înlocuirea unui text cu altul

SELECT REPLACE('180 Main Street', 'Street', 'St');
180 Main St
Curățarea datelor în baze de date PostgreSQL

Utilizarea funcției LPAD()

LPAD(input_string, length [, fill_value]) - adăugarea de text la începutul unui șir

SELECT LPAD('123', 7, 'X');
XXXX123
Curățarea datelor în baze de date PostgreSQL

Construirea interogării de curățare a șirurilor

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       | ...
 ...                             | ...   | ...                                   | ...          | ...
Curățarea datelor în baze de date PostgreSQL

Să exersăm!

Curățarea datelor în baze de date PostgreSQL

Preparing Video For Download...