Coderhouse goes global

🌍

Read more

What Is Data Imputation and How Analysts Handle Missing Values

AI Engineering

Build and ship real applications with LLMs, agents and automation.

8 weeks · Live online · View course →

Data Analytics

Go from zero to job-ready analyst with SQL, Python and BI tools.

10 weeks · Live online · View course →

Dan Patiño

AI Strategy & Innovation at Coderhouse

Data

What Is Data Imputation and How Analysts Handle Missing Values

What Is Data Imputation and How Analysts Handle Missing Values

Missing data imputation is the process of replacing empty, null, or unknown values in a dataset with reasonable substitutes so the data can be analyzed or fed into a model. Instead of throwing away every row with a blank cell, you fill the gap with an estimate: a median, the most frequent category, a value predicted from other columns, or a flag that says “this was missing.”

Why it matters now: almost every real dataset has holes. Forms get skipped, sensors drop out, CRM fields are optional, and joins between systems leave gaps. Many analysis tools and machine learning libraries either crash on missing values or silently drop rows, which can shrink your sample and bias your results. Knowing how to handle missing values deliberately is one of the most practical skills a data analyst can have, and it is a core step in any data cleaning workflow in Python and pandas.

Why values go missing: the three mechanisms

Before choosing a technique, ask why the data is missing. Statisticians describe three mechanisms, popularized by Donald Rubin and explained in depth in Stef van Buuren’s open book Flexible Imputation of Missing Data:

  • Missing completely at random (MCAR): the chance a value is missing has nothing to do with any data, observed or not. Example: a random server error drops some survey responses. Dropping those rows reduces power but does not bias results.

  • Missing at random (MAR): missingness depends on other observed columns. Example: younger users skip the income question more often. If you account for age, you can estimate income reasonably well.

  • Missing not at random (MNAR): missingness depends on the missing value itself. Example: people with very high incomes refuse to report them. No simple fill fixes this; you need domain knowledge, sensitivity analysis, or better data collection.

You rarely know the mechanism for sure, but thinking it through tells you how risky a quick fix will be.

Step one: measure the missingness

Start by profiling. In pandas, df.isna().sum() counts missing values per column, and df.isna().mean() gives the share. The pandas guide to working with missing data explains how NaN, None, and NA behave. Look for:

  • Columns that are mostly empty (often better dropped or redesigned than imputed).

  • Patterns, such as values missing together or only for one region, channel, or date range.

  • Fake missing values: placeholders like 0, -1, “N/A,” or 1900-01-01 that should be treated as missing.

Common imputation techniques

Deletion (listwise or pairwise)

Dropping rows with missing values is simple and fine when only a small share is missing and the data looks MCAR. It becomes dangerous when missingness is large or systematic, because you lose information and may skew the sample.

Mean, median, or mode

Replace numeric gaps with the column mean or, more robustly, the median; replace categorical gaps with the most frequent value. It is fast and keeps every row, but it shrinks variance and weakens relationships between variables. Medians are safer when the column has skew or outliers.

Constant value plus a missing indicator

Fill with a constant (such as “Unknown” or 0) and add a binary column that marks which rows were imputed. This keeps the information that a value was missing, which is often predictive in itself: a blank phone number field can say a lot about lead quality.

Forward fill and interpolation for time series

For ordered data like daily metrics or sensor readings, carrying the last known value forward or interpolating between neighbors respects the time structure better than a global average. Be careful across long gaps, where you may be inventing a trend that never happened.

Model-based imputation

K-nearest neighbors imputation fills a value using similar rows; iterative imputation predicts each column from the others in rounds. Both are available in scikit-learn, whose imputation documentation covers SimpleImputer, KNNImputer, IterativeImputer, and MissingIndicator. These methods preserve relationships better but cost more compute and need careful validation.

Multiple imputation

Instead of one filled dataset, multiple imputation creates several plausible versions, runs the analysis on each, and pools the results. It is the standard in research settings because it reflects the uncertainty that missing data introduces, rather than pretending the filled values are known.

Imputation in machine learning pipelines

When you train a model, fit the imputer on the training data only and then apply it to validation and test data. If you compute a median on the full dataset first, information from the test set leaks into training and your evaluation looks better than reality. Putting the imputer inside a pipeline, alongside scaling and encoding, makes this automatic and keeps production behavior identical to training.

Some algorithms, such as certain gradient boosting implementations, can handle missing values natively. Even then, a missing indicator and a clear understanding of why values are absent usually improve results.

Common mistakes to avoid

  • Imputing the target variable. If the outcome you are predicting is missing, drop those rows from training rather than inventing labels.

  • Filling everything with zero. Zero is a real value. Using it for “unknown revenue” or “unknown age” distorts averages and models.

  • Hiding the decision. Document which columns were imputed, how, and what share of values were affected, so stakeholders can judge the result.

  • Skipping a sensitivity check. Compare key results with and without imputation, or under two methods. If conclusions flip, the missing data is driving your story.

A quick checklist for analysts

  1. Count missing values per column and look for patterns.

  2. Convert placeholder values into true missing values.

  3. Think about the likely mechanism: MCAR, MAR, or MNAR.

  4. Choose the simplest method that fits, and add missing indicators when missingness may carry signal.

  5. Fit imputers on training data only, inside a pipeline.

  6. Report what you imputed and test whether conclusions hold.

FAQ

What percentage of missing data is too much to impute?

There is no universal cutoff. Small shares (a few percent) are usually easy to handle. When a column is missing for most rows, imputation mostly reflects your assumptions, so consider dropping the column or fixing collection instead.

Is the median better than the mean for imputation?

For skewed numeric data, such as income or order value, the median is usually safer because extreme values do not pull it. For roughly symmetric data, the two give similar results.

Can AI tools impute missing data for me?

AI assistants can suggest and write imputation code quickly, but they cannot know why your values are missing. The judgment about mechanism, method, and reporting still belongs to the analyst.

Learn to clean and prepare real data with live practice

If you want hands-on practice turning messy datasets into reliable analysis and models, Coderhouse offers live online courses with one class per week:

  • Data Analytics Course: 11 weeks for career switchers, analysts, and data-driven professionals, covering metrics, SQL, Python, visualization, experimentation, and AI-assisted analysis.

  • AI Engineering Course: 8 weeks for experienced developers moving into AI engineering, covering LLM applications, retrieval, agents, evaluation, and deployment.

Dan Patiño

I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.