How to replace NaN values in a dataframe column
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Replacing missing values in a single pandas column is one of the most common data-cleaning tasks. The right replacement depends on what the column means, because filling a missing category with "unknown" is very different from filling a missing measurement with a median or a forward-filled value.
Basic Replacement with fillna
If you already know the value you want, fillna is the direct tool:
Output:
This is the clearest approach when the replacement is a fixed scalar value.
Replacing Numeric NaN Values with a Statistic
For numeric columns, a constant is not always the best choice. A common pattern is to fill missing values with the mean or median of the non-missing data.
Median is often safer than mean when outliers exist. The important part is that you compute the statistic from the real data rather than hard-coding a guess.
Forward Fill and Backward Fill
Some columns are sequential, such as timestamps, sensor values, or labels that persist until changed. In those cases, carrying nearby values forward or backward may be more sensible than using a global average.
Backward fill works the other way:
Use these only when row order has meaning. On an unsorted table, forward-filling can silently create bad data.
Conditional Replacement
Sometimes you do not want one universal fill rule. You may want to fill values differently based on other columns or specific row conditions.
This pattern is useful when missing values mean different things in different subgroups.
Preserving Data Types
Type handling is where many quick fixes go wrong. A numeric column with missing values may already have been upcast to floating point because classic NumPy integer arrays do not support NaN.
If you want nullable integer behavior, use pandas extension dtypes:
Output:
That is better than accidentally converting an integer column into generic object data or losing the intended dtype during cleanup.
Fill One Column Without Touching the Whole DataFrame
It is common to see code like:
That fills every column, which may be too broad. If only one column should change, keep the operation scoped:
This is safer and easier to review.
When You Should Not Fill Missing Values
Not every NaN should be replaced. Sometimes the missingness itself carries information. For example:
- a missing shipped date may mean the order is still pending
- a missing lab result may mean the test was not performed
- a missing category may signal an upstream data issue
In those cases, blindly filling values can hide a real modeling or business problem. Good imputation starts with understanding why the value is missing.
Common Pitfalls
The most common mistake is filling an entire DataFrame with one value when only one column should be changed. That often corrupts columns that need different handling.
Another issue is using forward fill on unsorted or unrelated rows. ffill() depends on row order, so it only makes sense when the order reflects real sequence meaning.
Developers also often ignore dtype changes. Filling missing numeric values in a nullable integer column can produce a different dtype if you are not careful.
Finally, do not pick an imputation rule just because it is easy to code. Mean, median, constant values, and carry-forward all imply different assumptions about the data.
Summary
- Use
fillnafor direct replacement of missing values in a pandas column. - For numeric columns, median or mean fills are often better than arbitrary constants.
- Use
ffillorbfillonly when row order is meaningful. - Scope the operation to the target column instead of filling the entire DataFrame by accident.
- Check the resulting dtype and make sure the replacement strategy matches the meaning of the missing data.

