Hantera saknade värden

Datarensning i PostgreSQL-databaser

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Saknade data (ett exempel)

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

Representationer för saknade värden:

  • NULL (generellt)
  • '' - tom sträng (används för strängkolumner)
Datarensning i PostgreSQL-databaser

Orsaker till saknade data

Vad orsakar saknade data? Svart pussel med en bit som saknas

  • En bild av en hjärna i en huvudsiluett mänskliga fel
  • En bild av kugghjul systemrelaterade problem
Datarensning i PostgreSQL-databaser

Typer av saknade data

Tre bilder av olika typer av saknade data med respektive kategori och förkortning

Datarensning i PostgreSQL-databaser

Typer av saknade data

Tre bilder av olika typer av saknade data med respektive kategori och förkortning, samt beskrivning av Missing Completely at Random

Datarensning i PostgreSQL-databaser

Typer av saknade data

Tre bilder av olika typer av saknade data med respektive kategori och förkortning, samt beskrivning av Missing at Random

Datarensning i PostgreSQL-databaser

Typer av saknade 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        | ...
 ... | ...              | ...   | ...                                          | ...
Datarensning i PostgreSQL-databaser

Typer av saknade data

Tre bilder av olika typer av saknade data med respektive kategori och förkortning, samt beskrivning av Missing Not at Random

Datarensning i PostgreSQL-databaser

Identifiera saknade data

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
Datarensning i PostgreSQL-databaser

Identifiera saknade data

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
Datarensning i PostgreSQL-databaser

Åtgärda saknade data

  • Bästa alternativet: hitta och lägg till saknade värden

    • Kanske inte genomförbart
    • Kanske inte värt ansträngningen
  • Ange ett ersättningsvärde (medelvärde, median m.m.)

  • Uteslut poster

Datarensning i PostgreSQL-databaser

Ersätta saknade värden med COALESCE()

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

SELECT
  name,
  COALESCE(score, -1),
  inspection_type
FROM
  restaurant_inspection;
Datarensning i PostgreSQL-databaser

Ersätta saknade värden med 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        | ...
 ... | ...              | ...   | ...                                          | ...
Datarensning i PostgreSQL-databaser

Nu kör vi en övning!

Datarensning i PostgreSQL-databaser

Preparing Video For Download...