How to Replace NaN with Zero in Pandas

You can replace NaN with zero using either fillna(0) in Pandas or replace(np.nan,0) in Numpy.

Single Column:

Method 1: df['Column_name'].fillna(0)

Method 2: df['Column_name'].replace(np.nan,0)

Whole dataframe:

Method 1: df.fillna(0)

Method 2: df.replace(np.nan,0)


Example 1: single column

The following Python code first creates a dataframe with NaN in both columns and then replaces NaN in the first column with 0 using fillna(0).

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 300,500],
'Col_2': [np.nan, 30, 100, 88, 354,np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with 0 using fillna()
df['Col_1']=df['Col_1'].fillna(0)

# print out the updated dataframe
print('Dataframe removed NaN in first column: \n', df)

The following shows both versions of the dataframe, before being removed and after being removed. You can see that the NaN have been removed in the first column in the updated version of the dataframe.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  300.0  354.0
5  500.0    NaN

Dataframe removed NaN in first column: 
    Col_1  Col_2
0  100.0    NaN
1    0.0   30.0
2  200.0  100.0
3    0.0   88.0
4  300.0  354.0
5  500.0    NaN

Example 2: single column

The following first creates a dataframe with NaN in both columns and then replaces NaN in the first column with 0 using replace(np.nan,0).

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 300,500],
'Col_2': [np.nan, 30, 100, 88, 354,np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with 0 using replace()
df['Col_1']=df['Col_1'].replace(np.nan,0)

# print out the updated dataframe
print('Dataframe removed NaN in first column: \n', df)

The following is the output showing both before and after versions.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  300.0  354.0
5  500.0    NaN

Dataframe removed NaN in first column: 
    Col_1  Col_2
0  100.0    NaN
1    0.0   30.0
2  200.0  100.0
3    0.0   88.0
4  300.0  354.0
5  500.0    NaN

Example 3: whole dataframe

The following code will replace NaN with 0 for both columns using fillna(0).

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 300,500],
'Col_2': [np.nan, 30, 100, 88, 354,np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with 0
df=df.fillna(0)

# print out the updated dataframe
print('Dataframe removed NaN: \n', df)

The following is the updated version of data frame. As we can see, both columns have no NaN anymore.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  300.0  354.0
5  500.0    NaN

Dataframe removed NaN: 
    Col_1  Col_2
0  100.0    0.0
1    0.0   30.0
2  200.0  100.0
3    0.0   88.0
4  300.0  354.0
5  500.0    0.0

Example 4: whole dataframe

The following code will replace NaN with 0 for both columns using replace(np.nan,0).

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 300,500],
'Col_2': [np.nan, 30, 100, 88, 354,np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with 0 using replace()
df=df.replace(np.nan,0)

# print out the updated dataframe
print('Dataframe removed NaN: \n', df)

The following is the output showing two versions of dataframe, before and after removing NaN.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  300.0  354.0
5  500.0    NaN

Dataframe removed NaN: 
    Col_1  Col_2
0  100.0    0.0
1    0.0   30.0
2  200.0  100.0
3    0.0   88.0
4  300.0  354.0
5  500.0    0.0

Further Reading

The following is a tutorial showing how to drop rows or columns with NaN in Pandas.