Handling Missing Data in Pandas: NaN, fillna, and dropna (2026)
Handling Missing Data in Pandas comes down to two honest choices: drop the incomplete row, or fill the gap with something defensible. Module 9 covered building sequences from scratch. This guide covers cleaning up the sequences real data actually hands over, gaps included, and a specific Pandas 3.0 change to the exact method most tutorials still show as a simple one-liner.
Quick Answer: Handling Missing Data in Pandas
Handling missing data in pandas means finding gaps with isna(), then choosing between two responses: dropna() to remove rows or columns that are missing too much to trust, or fillna() to replace the gap with a constant, a computed statistic, or a neighboring value. As of Pandas 3.0, chaining an inplace call onto a column selection, such as df["col"].fillna(0, inplace=True), no longer updates anything at all, a direct consequence of the copy-on-write default this cluster has followed since Article 6.
The fillna That Does Nothing
A cleaning script needs to fill missing order amounts with zero before totaling anything up. The line looks completely standard, copied from a dozen tutorials without a second thought.
import pandas as pd import numpy as np orders = pd.DataFrame({ "customer": ["Alex", "Alex", "Sam", "Sam", "Priya", "Jordan", "Kim"], "item": ["mug", "pen", "mug", "notebook", "mug", np.nan, np.nan], "amount": [15.0, np.nan, 20.0, np.nan, 8.0, 25.0, np.nan] }) orders["amount"].fillna(0, inplace=True)
A warning appears, easy to miss in a busy terminal, and the data comes back out exactly as it went in:
print(orders["amount"].isna().sum()) # 3 still 3 missing values, nothing was filled
This is the exact copy-on-write guarantee this cluster has followed since Article 6, showing up in a new place. Any selection pandas hands back always behaves as a copy, so an inplace call chained onto that selection can never reach the original. The fix is one of two small changes:
orders.fillna({"amount": 0}, inplace=True)
# or
orders["amount"] = orders["amount"].fillna(0)
Finding Missing Data
Before fixing anything, pandas needs to be asked where the gaps actually are. isna() returns True wherever a value is missing. Summed per column, it becomes a quick count:
orders.isna().sum() # customer 0 # item 2 # amount 3 # dtype: int64
notna() is the inverse, True wherever a value is actually present. isnull() is an exact alias for isna(), same output, different name. Either is fine to use.
Removing It: dropna
By default, dropna() removes any row with even one missing value, in any column.
orders.dropna()
# customer item amount
# 0 Alex mug 15.0
# 2 Sam mug 20.0
# 4 Priya mug 8.0
Only three rows survive, everyone with both an item and an amount on record. Restricting the check to one column keeps rows that are only missing something elsewhere:
orders.dropna(subset=["amount"]) # customer item amount # 0 Alex mug 15.0 # 2 Sam mug 20.0 # 4 Priya mug 8.0 # 5 Jordan NaN 25.0
Jordan survives this time, missing an item but not an amount, exactly the row the default drop removed. thresh takes a different approach, setting a minimum number of non-missing values a row needs to survive:
orders.dropna(thresh=2) # keeps everyone except Kim, who has only 1 value out of 3
Filling It: fillna
Filling with a single constant is the simplest option:
orders.fillna({"amount": 0})
A statistic often makes more sense than an arbitrary number:
orders["amount"].fillna(round(orders["amount"].mean(), 2)) # 15.0, 17.0, 20.0, 17.0, 8.0, 25.0, 17.0
Forward fill and backward fill borrow a neighboring value instead of computing one:
orders["amount"].ffill() # carries the last known value forward: 15, 15, 20, 20, 8, 25, 25
What Changed in Pandas 3.0
One specific, testable change lands directly on this topic. Methods that support inplace, including fillna(), ffill(), bfill(), and interpolate(), now return the modified object itself instead of None when inplace=True is used.
result = orders.fillna({"amount": 0}, inplace=True)
print(result)
# the filled DataFrame itself, not None
That sounds small, but it used to mean an inplace call could never be chained. Calling anything on the result immediately failed with an AttributeError, since there was nothing but None to call it on. That specific limitation is gone as of Pandas 3.0, verified directly rather than assumed.
Dropping vs. Filling
Neither option is automatically correct. Dropping a row throws away everything else that row knew, not just the missing piece, which matters more the fewer rows there are to begin with. Filling keeps every row but invents a value that was never actually observed, which can quietly distort an average or a trend if too much of the column was empty to start with.
A missing value in one column out of thirty is usually safe to fill. A column that is half empty is a sign to ask why the data is missing before choosing either option, not a reason to reach for whichever method comes to mind first.
Mistakes That Give Away a Beginner
- Chaining fillna(inplace=True) or dropna(inplace=True) off a column selection, and not noticing the warning that says it changed nothing.
- Filling every column with the same constant regardless of what the column actually means, turning a missing price into a real price of zero.
- Dropping rows without checking how many are affected first. isna().sum() takes one line and prevents a lot of silently lost data.
- Assuming isna() and isnull() do different things. They are exact aliases.
- Filling with the mean of a column that has extreme outliers, which pulls the fill value away from what a typical row actually looks like.
Practice: Fix These Three Yourself
Count first
Run isna().sum() on the orders table and confirm which column has the most missing values before deciding anything.
Three kinds of drop
Compare the row counts of dropna(), dropna(subset=["amount"]), and dropna(thresh=2) on the same table, and explain why each number differs.
Fix the chain
Take orders["amount"].fillna(0, inplace=True) and rewrite it two different ways so it actually updates the original DataFrame.
Frequently Asked Questions
What's the difference between fillna and dropna in pandas?
dropna() removes rows or columns that contain missing values. fillna() keeps every row and replaces the missing values with something else, a constant, a statistic, or a neighboring value.
What's the difference between isna and isnull?
Nothing. isnull() is an exact alias for isna(). Both return the same True or False result wherever a value is missing.
Why doesn't df["col"].fillna(value, inplace=True) work anymore?
Under Pandas 3.0's copy-on-write default, selecting a column always behaves as a copy, so an inplace call chained onto that selection can never reach the original DataFrame. Use df.fillna({"col": value}, inplace=True) or reassign the column directly instead.
How do you fill missing values with the column mean?
Call fillna() with the mean already computed, such as df["col"].fillna(df["col"].mean()).
What does dropna(thresh=2) do?
It keeps only the rows that have at least 2 non-missing values, regardless of which columns they are in, and drops any row with fewer.
Does fillna with inplace=True still return None in pandas 3.0?
No. As of Pandas 3.0, fillna() and the other inplace-capable methods return the modified object itself instead of None, so the result can be chained further.
Conclusion: Two Honest Responses
Missing data has exactly two honest responses: drop the row, or fill the gap with something defensible. Everything in this article is a variation on deciding which one fits, and Pandas 3.0 finally makes the inplace version of either choice behave the way it always looked like it should.
Practice the three exercises above, then move to the next article in this cluster, which covers vectorization: writing pandas code that runs as one fast pass instead of a slow, row by row loop.
Next Up: Vectorization in Pandas
Writing fast code and avoiding loops, the pattern this cluster has been building toward.
▶ Read the Vectorization Guide- Working with Missing Data, pandas user guide
- What's New in Pandas 3.0.0, pandas.pydata.org
- Copy-on-Write User Guide, pandas.pydata.org
- Review Publically, List Comprehensions and Generator Expressions
- Review Publically, Pandas Series and DataFrames: A Complete Guide
- Review Publically, Python for Data Science: The Complete Guide
Khalid Hussain
Founder of Review Publically. MSc holder and Google Advanced Data Analytics certified. Teaches Python and SQL data analysis with a focus on current, correct, production-ready code rather than outdated conventions.
Related Articles