Handling Missing Data
Explore strategies for handling missing data within datasets by diagnosing missingness patterns and selecting appropriate treatments like deletion, median imputation, or model-based methods. Understand how to preserve data integrity and the impact of each method on your analysis and modeling outcomes.
A hospital readmission table can contain a recorded readmitted outcome for every patient while lab_value is absent for several patients. That absence is not harmless. Missingness means a field has no recorded value, commonly represented by NaN or NULL.
The first decision is not how to fill the blank. We first ask whether lab_value is absent randomly or whether the absence relates to patient age, clinical process, or readmission. If missing lab results occur more often among patients who are readmitted, removing those rows changes the population used for later analysis.
The code below compares the original patient count, missing-value counts, readmission patterns, and the rows remaining after dropna() removes every patient without a recorded lab result.
Lines 5–28:
build_patient_df()builds a small patient table withreadmittedrecorded for every patient andlab_valuemissing for some patients.Lines 34–38: The code prints the patient table and the number of missing values in each column.
Lines 41–42:
df["lab_value"].isna()builds a missingness indicator for the lab, and grouping that boolean byreadmittedwith.mean()yields the missing-lab rate separately forFalseandTrueoutcomes.Lines 47–59:
dropna()removes every row with any missing value, and the final 2-row summary makes the impact visible by comparing both the ...