處理遺漏值

清理 PostgreSQL 資料庫中的資料

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

遺漏資料(範例)

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

遺漏值的表示法:

  • NULL(通用)
  • ''-空字串(用於字串欄位)
清理 PostgreSQL 資料庫中的資料

遺漏資料的成因

為何會有遺漏資料?缺一塊的黑色拼圖

  • 頭像中的人腦圖示 人為錯誤
  • 齒輪圖示 系統性問題
清理 PostgreSQL 資料庫中的資料

遺漏資料的型態

三種遺漏資料型態與縮寫的總覽圖

清理 PostgreSQL 資料庫中的資料

遺漏資料的型態

三種遺漏資料型態與縮寫,並加入「完全隨機遺漏」說明的圖

清理 PostgreSQL 資料庫中的資料

遺漏資料的型態

三種遺漏資料型態與縮寫,並加入「隨機遺漏」說明的圖

清理 PostgreSQL 資料庫中的資料

遺漏資料的型態

 ... |       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        | ...
 ... | ...              | ...   | ...                                          | ...
清理 PostgreSQL 資料庫中的資料

遺漏資料的型態

三種遺漏資料型態與縮寫,並加入「非隨機遺漏」說明的圖

清理 PostgreSQL 資料庫中的資料

辨識遺漏資料

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
清理 PostgreSQL 資料庫中的資料

辨識遺漏資料

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
清理 PostgreSQL 資料庫中的資料

修正遺漏資料

  • 最佳作法:找出並補回遺漏值

    • 可能不可行
    • 可能不值得
  • 填入代表值(平均數、中位數等)

  • 排除該筆紀錄

清理 PostgreSQL 資料庫中的資料

用 COALESCE() 取代遺漏值

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

SELECT
  name,
  COALESCE(score, -1),
  inspection_type
FROM
  restaurant_inspection;
清理 PostgreSQL 資料庫中的資料

用 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        | ...
 ... | ...              | ...   | ...                                          | ...
清理 PostgreSQL 資料庫中的資料

一起來練習吧!

清理 PostgreSQL 資料庫中的資料

Preparing Video For Download...