偵測不一致的資料

清理 PostgreSQL 資料庫中的資料

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

不一致的資料

  • 某些餐廳稽查規則
  • score 代表違規次數 危害等級圖示
    camis    |              name               |  score | ... 
    ---------+---------------------------------+--------+-----
    ...      | ...                             | ...    | ...                                   
    41659848 | LA BRISA DEL CIBAO              | 20     | ...
    40961447 | MESON SEVILLA RESTAURANT        | 50     | ...
    50063071 | WA BAR                          | 15     | ...
    ...      | ...                             | ...    | ...
    
  • A(0 到 13)、B(14 到 27)、C(28 以上)
  • 等第情境:
    • 初次稽查拿到 A
    • 複查可能為 ABC
1 https://www1.nyc.gov/assets/doh/downloads/pdf/rii/restaurant-grading-faq.pdf
清理 PostgreSQL 資料庫中的資料

用 SQL 檢查規則

  • 相依規則可能造成不一致
  • 可用 SQL 編碼規則
  • A 對應分數 0 到 13
SELECT 
    camis, 
    grade, 
    grade_date, 
    score, 
    inspection_type 
FROM 
    restaurant_inspection 
WHERE 
    grade = 'A' AND 
    score NOT BETWEEN 0 AND 13;
0
清理 PostgreSQL 資料庫中的資料

用 SQL 檢查規則

  • B 對應分數 14 到 27
SELECT 
    camis, 
    grade, 
    grade_date, 
    score, 
    inspection_type 
FROM 
    restaurant_inspection 
WHERE 
    grade = 'B' AND 
    score NOT BETWEEN 14 AND 27;
 camis    | grade | grade_date | score |         inspection_type          
 ---------+-------+------------+-------+----------------------------------
 50034653 | B     | 12/06/2019 | -1    | Cycle Inspection / Re-inspection
清理 PostgreSQL 資料庫中的資料

用 SQL 檢查規則

SELECT 
    camis, grade, grade_date, score, inspection_type FROM 
    restaurant_inspection 
WHERE 
    (grade = 'A' OR grade = 'B' OR grade = 'C') AND
    inspection_type LIKE '%Reopening%';
 camis    | grade | grade_date | score |                 inspection_type                 | ... 
 ---------+-------+------------+-------+-------------------------------------------------+-----
 ...      | ...   | ...        | ...   | ...                                             | ...
 50005784 | C     | 05/29/2019 | 14    | Cycle Inspection / Reopening Inspection         | ...
 50091190 | C     | 07/12/2019 | 7     | Pre-permit (Operational) / Reopening Inspection | ...
 40395023 | C     | 09/13/2019 | 8     | Cycle Inspection / Reopening Inspection         | ...
 50037770 | C     | 10/26/2018 | 11    | Cycle Inspection / Reopening Inspection         | ...
 50036406 | C     | 07/10/2018 | 20    | Cycle Inspection / Reopening Inspection         | ...
 ...      | ...   | ...        | ...   | ...                                             | ...
1 https://www1.nyc.gov/assets/doh/downloads/pdf/rii/restaurant-grading-faq.pdf
清理 PostgreSQL 資料庫中的資料

資料清理洞見

  • 多元作法
  • 需審慎思考
  • 領域知識關鍵
    • 哪些值有效
    • 為何會重複
    • 合適的填補值
清理 PostgreSQL 資料庫中的資料

一起來練習吧!

清理 PostgreSQL 資料庫中的資料

Preparing Video For Download...