Zpracování chybějících dat

Cleaning Data in PostgreSQL Databases

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Chybějící data (příklad)

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

Reprezentace chybějících hodnot:

  • NULL (obecně)
  • '' – prázdný řetězec (pro řetězcové sloupce)
Cleaning Data in PostgreSQL Databases

Příčiny chybějících dat

Co způsobuje chybějící data? Černé puzzle s chybějícím dílkem

  • Obrázek lidského mozku v siluetě hlavy lidská chyba
  • Obrázek ozubených kol systémové problémy
Cleaning Data in PostgreSQL Databases

Typy chybějících dat

Tři obrázky různých typů dat s kategorií a zkratkou

Cleaning Data in PostgreSQL Databases

Typy chybějících dat

Tři obrázky různých typů dat s kategorií, zkratkou a popisem Missing Completely at Random

Cleaning Data in PostgreSQL Databases

Typy chybějících dat

Tři obrázky různých typů dat s kategorií, zkratkou a popisem Missing at Random

Cleaning Data in PostgreSQL Databases

Typy chybějících dat

 ... |       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        | ...
 ... | ...              | ...   | ...                                          | ...
Cleaning Data in PostgreSQL Databases

Typy chybějících dat

Tři obrázky různých typů dat s kategorií, zkratkou a popisem Missing Not at Random

Cleaning Data in PostgreSQL Databases

Identifikace chybějících dat

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
Cleaning Data in PostgreSQL Databases

Identifikace chybějících dat

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
Cleaning Data in PostgreSQL Databases

Řešení chybějících dat

  • Nejlepší možnost: dohledat a doplnit chybějící hodnoty

    • Nemusí být proveditelné
    • Nemusí být přínosné
  • Doplnit hodnotu (průměr, medián apod.)

  • Vyloučit záznamy

Cleaning Data in PostgreSQL Databases

Nahrazení chybějících hodnot pomocí COALESCE()

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

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

Nahrazení chybějících hodnot pomocí 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        | ...
 ... | ...              | ...   | ...                                          | ...
Cleaning Data in PostgreSQL Databases

Lass uns üben!

Cleaning Data in PostgreSQL Databases

Preparing Video For Download...