How to Use fillna in pandas to Handle Missing Data
In pandas, use
fillna() to replace missing values (NaN) in a DataFrame or Series with a specified value or method. This helps clean data by filling gaps with constants, forward-fill, or backward-fill. The method returns a new object or modifies in place if specified.Syntax
The fillna() method has several parameters:
value: The value to replace missing data with (number, string, dict, or Series).method: Use'ffill'(forward fill) or'bfill'(backward fill) to propagate non-missing values.axis: Choose rows (0) or columns (1) to fill along.inplace: IfTrue, modifies the original object instead of returning a new one.limit: Maximum number of consecutive NaNs to fill.
python
DataFrame.fillna(value=None, method=None, axis=0, inplace=False, limit=None)
Example
This example shows how to fill missing values in a DataFrame with a constant and with 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 NaN with a constant value filled_constant = df.fillna(0) # Fill NaN using forward fill method filled_ffill = df.fillna(method='ffill') print('Original DataFrame:') print(df) print('\nFill NaN with 0:') print(filled_constant) print('\nFill NaN with forward 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 NaN with 0:
A B
0 1.0 0.0
1 0.0 2.0
2 3.0 0.0
3 0.0 4.0
Fill NaN with 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 using fillna() include:
- Using
methodandvaluetogether, which is not allowed. - Forgetting to set
inplace=Trueif you want to modify the original DataFrame. - Not specifying
limitwhen you want to restrict how many NaNs get filled. - Trying to fill with incompatible types (e.g., filling numeric columns with strings).
python
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, np.nan, 3]}) # Wrong: Using value and method together raises error try: df.fillna(value=0, method='ffill') except ValueError as e: print(f'Error: {e}') # Right: Use either value or method filled = df.fillna(method='ffill') print(filled)
Output
Error: Cannot specify both 'value' and 'method'
A
0 1.0
1 1.0
2 3.0
Quick Reference
Here is a quick summary of fillna() options:
| Parameter | Description | Example |
|---|---|---|
| value | Value to replace NaNs | df.fillna(0) |
| method | Fill method: 'ffill' or 'bfill' | df.fillna(method='ffill') |
| axis | Axis to fill along (0=rows, 1=columns) | df.fillna(0, axis=1) |
| inplace | Modify original DataFrame if True | df.fillna(0, inplace=True) |
| limit | Max number of NaNs to fill | df.fillna(0, limit=1) |
Key Takeaways
Use fillna() to replace missing values in pandas DataFrames or Series easily.
Choose between filling with a constant value or using methods like forward fill.
Do not use both value and method parameters together in fillna().
Set inplace=True to modify the original data directly.
Use limit to control how many consecutive NaNs get filled.