Dropping & Filling Missing Values (dropna, fillna)0%
Handling Duplicates & Data Type Conversions

Dropping & Filling Missing Values (dropna, fillna)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Dropping & Filling Missing Values (dropna, fillna)

Reading Time: 8 Mins | Level: Beginner to Advanced


1. Strategies for Handling Missing Data

Once missing values are identified, a Data Analyst has two primary remedies:

  1. 1
    Deletion / Dropping (dropna): Remove rows or columns containing nulls (best when missingness is tiny, e.g. < 2%).
  2. 2
    Imputation / Filling (fillna): Replace missing values with domain-appropriate surrogates (mean, median, mode, forward-fill, or a sentinel constant).

2. Dropping Missing Data (df.dropna)

Key Parameters:

  • how='any' (Default): Drops a row if even one column is null.
  • how='all': Drops a row only if all columns are null.
  • subset=['ColA', 'ColB']: Checks for nulls only in specified critical columns.
  • axis=1: Drops entire columns containing nulls.
Python
import pandas as pd
import numpy as np
 
df = pd.DataFrame({
'Name': ['Amit', 'Sunita', np.nan, 'Rohan'],
'Age': [24, np.nan, 29, 31],
'Salary': [50000, 62000, np.nan, 75000]
})
 
# Drop rows where 'Name' is null (essential identifier)
df_valid_names = df.dropna(subset=['Name'])
 
# Drop rows only if both Age and Salary are missing
df_clean = df.dropna(subset=['Age', 'Salary'], how='all')

3. Imputing Missing Values (df.fillna)

A. Filling with a Constant

Python
# Fill missing names with 'Unknown'
df['Name'] = df['Name'].fillna('Unknown')

B. Statistical Imputation (Mean & Median)

In financial and numerical datasets, imputing with the median is preferred over the mean because the median is robust to extreme outliers:

Python
# Impute Age with median age
median_age = df['Age'].median()
df['Age'] = df['Age'].fillna(median_age)
 
# Impute Salary with mean salary
df['Salary'] = df['Salary'].fillna(df['Salary'].mean())

C. Sequential Imputation: Forward Fill & Backward Fill

In time-series and sensor data, use ffill() (propagate last valid observation forward) or bfill() (propagate next valid observation backward):

Python
# Forward fill
df_filled = df.ffill()

Interactive Quiz

Multiple Choice Questions

Question 1
What is the default behavior of df.dropna()?
Question 2
How can you drop rows only when the 'Customer_Email' column is missing?
Question 3
Why is the median often preferred over the mean when imputing missing numeric values in skewed data (like salaries)?
Question 4
What does the df.ffill() method do?
Question 5
What parameter in fillna() allows updating the original DataFrame directly without reassigning?

Next Lesson

Handling Duplicates & Data Type Conversions

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext