处理缺失数据

在 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 数据库中清理数据

Passons à la pratique !

在 PostgreSQL 数据库中清理数据

Preparing Video For Download...