Handling Missing Data

You've just delivered a critical sales forecast to the executive team. The numbers look promising, but the CEO points out a sudden dip in a key region's projection that doesn't align with recent market trends. Could your analysis be misleading due to incomplete data, or is there a genuine problem you missed? Often, the silent culprit behind such discrepancies is missing data, subtly skewing our insights and decisions.

In Pandas, missing data primarily manifests as NaN (Not a Number) within DataFrames and Series. This special floating-point value indicates an undefined or unrepresentable numerical value, but Pandas uses it as a general placeholder for any missing data type. Missing values can arise from various sources: data entry errors, absent observations during collection, or even failed merges between datasets where no corresponding record exists.

pythonA DataFrame with Missing Values

Identifying Missing Values

Before addressing missing data, you must first locate it. Pandas provides two primary methods for this: isnull() and notnull(). The df.isnull() method returns a boolean DataFrame of the same shape as the original, where True indicates a missing value and False indicates a present value. Conversely, df.notnull() returns True for present values and False for missing ones, effectively serving as the inverse of isnull().

pythonUsing isnull() and notnull()

Quantifying Missing Data

Knowing the exact count of missing values is a critical step before deciding on a handling strategy. A few missing entries might be negligible, but a column with 80% missing data requires a different approach. Quantifying missing values helps you assess the severity of the problem, understand which columns are most affected, and determine if a column is usable for analysis or needs significant intervention.

pythonCounting Missing Values per Column and Total
Check Your Understanding
If df.isnull().sum() shows 'Sales 50' for a DataFrame with 100 rows, what does this imply?

Removing Missing Data with dropna()

One direct way to handle missing data is to remove the rows or columns that contain it using the df.dropna() method. This method offers flexibility through its parameters. The how parameter specifies whether to drop a row/column if any value is missing (default) or if all values are missing. The axis parameter dictates whether to drop rows (axis=0) or columns (axis=1). By default, dropna() returns a new DataFrame; to modify the DataFrame in place, set inplace=True.

pythonApplying dropna() to Remove Rows
⚠️ Caution: Data Loss with dropna()

While dropna() is straightforward, it can lead to significant data loss if many rows or columns contain missing values. Removing a large portion of your dataset can reduce the statistical power of your analysis and potentially discard valuable, complete information from other columns in the removed rows. Always quantify missing data first to understand the impact of dropping.

Imputing Missing Data with fillna()

Instead of removing data, imputation involves replacing missing values with substitute values. The df.fillna() method in Pandas is your tool for this. You can fill NaNs with a constant value (e.g., 0 or 'Unknown'), or with a calculated statistic like the mean, median, or mode of the column. For time-series or ordered data, ffill (forward fill) propagates the last valid observation forward, while bfill (backward fill) propagates the next valid observation backward.

pythonApplying fillna() with Different Strategies
Check Your Understanding
For a column representing customer age, which fillna() strategy is generally most appropriate to avoid skewing statistical analysis?
Comparing dropna() and fillna() Strategies
Featuredropna()fillna()
PurposeRemoves rows/columns with missing dataReplaces missing data with specified values
Impact on Data SizeReduces DataFrame size, potential for significant data lossMaintains DataFrame size, no data loss
Risk of BiasLow risk of introducing artificial bias (if data is MAR/MCAR)High risk of introducing bias if imputation strategy is poor
Ideal Use CaseWhen missing data is minimal or rows are truly irrelevant; for complete case analysisWhen data loss is unacceptable; for maintaining dataset size and statistical power
FlexibilityLimited options (any/all, axis)Highly flexible (constant, mean, median, mode, ffill, bfill, interpolation)
A side-by-side comparison of the trade-offs between removing and imputing missing data.
Try It Yourself
Modify the df_exercise DataFrame. First, remove any rows where all values are missing. Then, fill the missing values in the 'Price' column with the median of the 'Price' column. Finally, replace any missing values in the 'Category' column with the string 'Miscellaneous'.
python
Key Takeaways
  • Identification: Use df.isnull() and df.notnull() to pinpoint missing values, which appear as NaN.

  • Quantification: df.isnull().sum() provides counts of missing values per column, helping assess data quality and severity.

  • Removal (Caution): df.dropna() removes rows or columns with NaNs, but be mindful of potential data loss, especially with how='any'.

  • Imputation (Strategy): df.fillna() replaces NaNs with chosen values (e.g., constant, mean, median, mode, ffill/bfill), requiring careful strategy selection.

  • Impact: Thoughtful handling of missing data is vital for accurate analysis, preventing misleading insights like the skewed sales forecast from the opening scenario.

← All lessons in Pandas Basics

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free