How to Replace NaN with Zero in pandas DataFrame
Use the
DataFrame.fillna(0) method in pandas to replace all NaN values with zero. This method works on the entire DataFrame or on specific columns to fill missing values easily.Syntax
The basic syntax to replace NaN values with zero in a pandas DataFrame is:
DataFrame.fillna(0): Replaces allNaNvalues with zero in the entire DataFrame.DataFrame['column'].fillna(0): ReplacesNaNvalues with zero in a specific column.
python
df.fillna(0) df['column_name'].fillna(0)
Example
This example shows how to replace NaN values with zero in a pandas DataFrame. It demonstrates replacing missing values in the whole DataFrame and in a specific column.
python
import pandas as pd import numpy as np data = {'A': [1, 2, np.nan, 4], 'B': [np.nan, 2, 3, 4]} df = pd.DataFrame(data) # Replace NaN with zero in entire DataFrame df_filled = df.fillna(0) # Replace NaN with zero in column 'A' df['A_filled'] = df['A'].fillna(0) print('Original DataFrame:') print(df) print('\nDataFrame after fillna(0):') print(df_filled) print('\nColumn A after fillna(0):') print(df['A_filled'])
Output
Original DataFrame:
A B
0 1.0 NaN
1 2.0 2.0
2 NaN 3.0
3 4.0 4.0
DataFrame after fillna(0):
A B
0 1.0 0.0
1 2.0 2.0
2 0.0 3.0
3 4.0 4.0
Column A after fillna(0):
0 1.0
1 2.0
2 0.0
3 4.0
Name: A_filled, dtype: float64
Common Pitfalls
One common mistake is trying to replace NaN values without assigning the result back to the DataFrame or column. The fillna() method returns a new object by default and does not change the original data unless inplace=True is used.
Another pitfall is using fillna(0) on non-numeric columns where zero might not make sense.
python
import pandas as pd import numpy as np data = {'A': [1, np.nan, 3]} df = pd.DataFrame(data) # Wrong: does not change original DataFrame _df_wrong = df.fillna(0) print('Original DataFrame after fillna without assignment:') print(df) # Right: assign back or use inplace df_correct = df.fillna(0) print('\nDataFrame after fillna with assignment:') print(df_correct) # Or inplace df.fillna(0, inplace=True) print('\nDataFrame after fillna with inplace=True:') print(df)
Output
Original DataFrame after fillna without assignment:
A
0 1.0
1 NaN
2 3.0
DataFrame after fillna with assignment:
A
0 1.0
1 0.0
2 3.0
DataFrame after fillna with inplace=True:
A
0 1.0
1 0.0
2 3.0
Quick Reference
Here is a quick summary of how to replace NaN values with zero in pandas:
| Method | Description | Example |
|---|---|---|
| fillna(0) | Replace all NaN in DataFrame with zero | df.fillna(0) |
| fillna(0, inplace=True) | Replace NaN in place without creating new DataFrame | df.fillna(0, inplace=True) |
| column.fillna(0) | Replace NaN in a specific column | df['A'].fillna(0) |
Key Takeaways
Use df.fillna(0) to replace all NaN values with zero in a DataFrame.
Remember to assign the result back or use inplace=True to modify the original data.
You can replace NaN values in specific columns using df['column'].fillna(0).
Avoid replacing NaN with zero in non-numeric columns where zero may not be meaningful.