Pandas dataframe fillna only some columns in place
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want to fill missing values in only some DataFrame columns, the safest pattern is to assign the filled subset back to those columns explicitly. The phrase "in place" often causes confusion in pandas because chained assignment and partial views can behave differently from what people expect.
Fill Only Selected Columns
The cleanest approach is to select the columns you want and assign the result back.
This modifies only the chosen columns and leaves the others alone.
Use Different Fill Values Per Column
You can also pass a dictionary when each column needs a different replacement.
That is often better than applying one scalar value to all selected columns.
Prefer Assignment Over inplace=True on a Slice
This is the pattern many people try first:
It looks reasonable, but it is not the best choice. The slice may be a temporary object, and inplace=True does not guarantee the original DataFrame is updated the way you intended.
Use this instead:
That assignment is explicit and predictable.
loc Is a Good Choice for Clarity
Many teams prefer loc because it makes the row and column selection clearer.
This is especially helpful when the code already uses loc for other transformations and you want to keep style consistent.
Fill by Type or Rule
Sometimes the target columns are chosen dynamically, for example all numeric columns.
This is useful in data-cleaning pipelines where column names are not always fixed.
Beware of Dtype Changes
Filling missing values can change or stabilize dtypes depending on the data. For example, a float column that contains NaN may stay float even if you fill with integers, because NaN handling already pushed it to floating-point representation.
If dtype matters, inspect it after filling:
For nullable integer support, consider pandas nullable dtypes such as Int64 instead of classic NumPy integer types when missing data is involved.
Common Pitfalls
The most common mistake is using inplace=True on a sliced subset and expecting it to safely mutate the original DataFrame. That pattern is fragile and can trigger confusing behavior.
Another issue is filling text and numeric columns with the same scalar value even though the columns need different defaults. A dictionary is usually clearer.
A third problem is ignoring dtype consequences after the fill operation. Cleaning the missing values is only half the job if later code depends on stable types.
Summary
- Fill selected columns by assigning the filled subset back to those columns.
- Use dictionaries when different columns need different replacement values.
- Prefer explicit assignment over
inplace=Trueon a slice. - '
locis a clear and reliable way to target subsets.' - Check dtypes after filling when downstream logic depends on them.

