How to Fill Missing Values in pandas DataFrames Easily
Use the
fillna() method in pandas to fill missing values in a DataFrame or Series. You can fill with a constant value, forward fill, backward fill, or use other methods to replace NaN values.Syntax
The fillna() method replaces missing values (NaN) in pandas objects. You can specify a value to fill, or use methods like forward fill or backward fill.
value: scalar or dict, value to replaceNaNmethod: {'ffill', 'bfill'}, fill using previous or next valid valueaxis: 0 or 1, fill along rows or columnsinplace: bool, whether to modify the original object
python
DataFrame.fillna(value=None, method=None, axis=None, inplace=False)
Example
This example shows how to fill missing values in a DataFrame using a constant value and forward fill method.
python
import pandas as pd import numpy as np data = {'A': [1, np.nan, 3, np.nan], 'B': [np.nan, 2, np.nan, 4]} df = pd.DataFrame(data) # Fill missing values with 0 filled_constant = df.fillna(0) # Forward fill missing values filled_ffill = df.fillna(method='ffill') print('Original DataFrame:') print(df) print('\nFill with 0:') print(filled_constant) print('\nForward fill:') print(filled_ffill)
Output
Original DataFrame:
A B
0 1.0 NaN
1 NaN 2.0
2 3.0 NaN
3 NaN 4.0
Fill with 0:
A B
0 1.0 0.0
1 0.0 2.0
2 3.0 0.0
3 0.0 4.0
Forward fill:
A B
0 1.0 NaN
1 1.0 2.0
2 3.0 2.0
3 3.0 4.0
Common Pitfalls
Common mistakes when filling missing values include:
- Using
fillna()withoutinplace=Trueand expecting the original DataFrame to change. - Filling with inappropriate values that distort data meaning.
- Using forward fill when the first rows are missing, which leaves them unchanged.
python
import pandas as pd import numpy as np df = pd.DataFrame({'A': [np.nan, 2, np.nan]}) # Wrong: fillna without inplace, original df unchanged filled_wrong = df.fillna(0) # Correct: either assign or use inplace # df = df.fillna(0) # or # df.fillna(0, inplace=True) print('Original DataFrame after fillna without inplace:') print(df) print('\nFilled DataFrame assigned to new variable:') print(filled_wrong)
Output
Original DataFrame after fillna without inplace:
A
0 NaN
1 2.0
2 NaN
Filled DataFrame assigned to new variable:
A
0 0.0
1 2.0
2 0.0
Quick Reference
Summary of common fillna() options:
| Parameter | Description |
|---|---|
value | Fill missing values with this scalar or dict |
method='ffill' | Fill missing values with previous valid value |
method='bfill' | Fill missing values with next valid value |
inplace=True | Modify the original DataFrame instead of returning a copy |
| Parameter | Description |
|---|---|
| value | Fill missing values with this scalar or dict |
| method='ffill' | Fill missing values with previous valid value |
| method='bfill' | Fill missing values with next valid value |
| inplace=True | Modify the original DataFrame instead of returning a copy |
Key Takeaways
Use pandas' fillna() method to replace missing values easily.
Specify a value or method like 'ffill' to control how missing data is filled.
Remember to assign the result or use inplace=True to update your DataFrame.
Choose fill values carefully to avoid changing data meaning.
Forward fill does not fill missing values at the start of data.