C.W.K.
Stream
Lesson 05 of 06 · published

Null 과 outlier 처리 — default 아니라 전략

~11 min · nulls, outliers, imputation

Level 0구경꾼
0 XP0/47 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Null 덮지 말고 먼저 이해해

"Null 있으니까 mean / 0 / forward-fill 로 채우자" 본능이 데이터 작업에서 가장 흔한 나쁜 반사. Null 은 항상 정보 가져 — 필드가 적용 안 돼서 missing, source 가 기록 안 해서 missing, 또는 버그라서 missing. 각 origin 이 다른 응답 필요하고 "mean 으로 채움" 은 거의 절대 옳은 거 아니야.

4가지 정당한 전략

  1. Row drop — row 가 그 필드 없이 쓸모없을 때. df.dropna(subset=['order_id', 'amount_usd']).
  2. Column drop — 필드가 거의 다 null 이고 제거해도 정보 손실 없을 때.
  3. Sentinel 으로 impute — 명시적 'unknown', -1, 또는 도메인 default — downstream 로직이 값 필요한데 missing 이 의미 있을 때.
  4. 통계로 impute — mean/median/forward-fill — 필드가 row 간 진정 호환 가능할 때만. 실전엔 드물고 자주 버그 가림.

Outlier — 같은 규율

Outlier 는 (a) 입력 에러 (amount = 99999999 인데 의도가 9999.99), (b) 정당하지만 비범한 값, 또는 (c) 본인이 탐지하려던 신호 자체 (fraud, anomaly) 일 수 있어. 자동 cap 또는 자동 제거 마. Surface 하고, count 하고, 파이프라인마다 뭘 의미하는지 결정.

Code

명시적 null/outlier 처리 — 한 거 로깅, silent fix 안 함·python
import logging
import pandas as pd

log = logging.getLogger('quality')

def handle_nulls(df: pd.DataFrame, required: list[str]) -> pd.DataFrame:
    n_before = len(df)
    df = df.dropna(subset=required)
    log.info('nulls.dropped=%d required=%s', n_before - len(df), required)
    return df

def handle_outliers(df: pd.DataFrame, col: str, low_q: float = 0.01, high_q: float = 0.99) -> pd.DataFrame:
    lo, hi = df[col].quantile([low_q, high_q])
    mask = df[col].between(lo, hi)
    n_drop = (~mask).sum()
    log.info('outliers.col=%s low_q=%.4f high_q=%.4f dropped=%d', col, lo, hi, n_drop)
    # 중요: drop 한 row 는 audit 위해 어딘가에 작성
    df.loc[~mask].to_parquet(f'audit/outliers_{col}.parquet', index=False)
    return df.loc[mask]

External links

Exercise

>5% null 인 column 하나 가진 DataFrame 골라. 그 column 에 대해 null 이 뭐 나타내는지 — count 가 아니라 row 봐 — 한 단락 메모. 그리고 위 4가지 중 옳은 전략 골라 — 답이 "upstream 소유자한테 물어봐 모르니까" 면 솔직히.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.