Omgaan met ontbrekende data

Data opschonen in PostgreSQL-databases

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Ontbrekende data (voorbeeld)

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     |       | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...

Weergaven voor ontbrekende waarden:

  • NULL (algemeen)
  • '' - lege string (voor tekstkolommen)
Data opschonen in PostgreSQL-databases

Oorzaken van ontbrekende data

Waardoor ontstaan ontbrekende data? Zwarte puzzel met ontbrekend stukje

  • Afbeelding van een menselijk brein in profiel menselijke fout
  • Afbeelding van tandwielen systematische issues
Data opschonen in PostgreSQL-databases

Typen ontbrekende data

Drie afbeeldingen van de typen data met elke categorie en afkorting

Data opschonen in PostgreSQL-databases

Typen ontbrekende data

Drie afbeeldingen van de typen data met elke categorie en afkorting; beschrijving van Missing Completely at Random toegevoegd

Data opschonen in PostgreSQL-databases

Typen ontbrekende data

Drie afbeeldingen van de typen data met elke categorie en afkorting; beschrijving van Missing at Random toegevoegd

Data opschonen in PostgreSQL-databases

Typen ontbrekende data

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     |       | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...
Data opschonen in PostgreSQL-databases

Typen ontbrekende data

Drie afbeeldingen van de typen data met elke categorie en afkorting; beschrijving van Missing Not at Random toegevoegd

Data opschonen in PostgreSQL-databases

Ontbrekende data identificeren

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
Data opschonen in PostgreSQL-databases

Ontbrekende data identificeren

SELECT
  inspection_type,
  COUNT(*) as count
FROM
  restaurant_inspection
WHERE
  score IS NULL
GROUP BY
  inspection_type
ORDER BY
  count DESC;
                  inspection_type                  | count 
 --------------------------------------------------+-------
 Administrative Miscellaneous / Initial Inspection |   104
 Smoke-Free Air Act / Initial Inspection           |    29
 Calorie Posting / Initial Inspection              |    22
 Administrative Miscellaneous / Re-inspection      |    22
 Trans Fat / Initial Inspection                    |    18
 Smoke-Free Air Act / Re-inspection                |     7
 Trans Fat / Re-inspection                         |     3
Data opschonen in PostgreSQL-databases

Ontbrekende data herstellen

  • Beste optie: vind en vul ontbrekende waarden aan

    • Kan niet haalbaar zijn
    • Misschien niet de moeite waard
  • Vul met een waarde (gemiddelde, mediaan, etc.)

  • Records uitsluiten

Data opschonen in PostgreSQL-databases

Ontbrekende waarden vervangen met COALESCE()

COALESCE(arg1, [arg2, ...])

SELECT
  name,
  COALESCE(score, -1),
  inspection_type
FROM
  restaurant_inspection;
Data opschonen in PostgreSQL-databases

Ontbrekende waarden vervangen met COALESCE()

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     | -1    | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...
Data opschonen in PostgreSQL-databases

Laten we oefenen!

Data opschonen in PostgreSQL-databases

Preparing Video For Download...