Replace NaN Values with Zeros in Pandas DataFrame

Depending on the scenario, you may use either of the 4 approaches below in order to replace NaN values with zeros in Pandas DataFrame:

(1) For a single column using Pandas: 

df['DataFrame Column'] = df['DataFrame Column'].fillna(0)

(2) For a single column using NumPy:

df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 0)

(3) For an entire DataFrame using Pandas:

df.fillna(0)

(4) For an entire DataFrame using NumPy:

df.replace(np.nan,0)

Let’s now review how to apply each of the 4 cases using simple examples.

4 cases to replace NaN values with zeros in Pandas DataFrame

Case 1: replace NaN values with zeros for a column using Pandas

Suppose that you have a single column with the following data that contains NaN values:

values
700
NaN
500
NaN

You can then create a DataFrame in Python to capture that data:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values': [700, np.nan, 500, np.nan]})

print (df)

Run the code in Python, and you’ll get the following DataFrame with the NaN values:

   values
0   700.0
1     NaN
2   500.0
3     NaN

In order to replace the NaN values with zeros for a column using Pandas, you may use the first approach introduced at the top of this guide:

df['DataFrame Column'] = df['DataFrame Column'].fillna(0)

In the context of our example, here is the complete Python code to replace the NaN values with 0’s:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values': [700, np.nan, 500, np.nan]})
df['values'] = df['values'].fillna(0)

print (df)

Run the code, and you’ll see that the previous two NaN values became 0’s:

   values
0   700.0
1     0.0
2   500.0
3     0.0

Case 2: replace NaN values with zeros for a column using NumPy

You can accomplish the same task, of replacing the NaN values with zeros, by using NumPy:

df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 0)

For our example, you can use the following code to perform the replacement:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values': [700, np.nan, 500, np.nan]})
df['values'] = df['values'].replace(np.nan, 0)

print (df)

As before, the two NaN values became 0’s:

   values
0   700.0
1     0.0
2   500.0
3     0.0

Case 3: replace NaN values with zeros for an entire DataFrame using Pandas

For the first two cases, you only had a single column in the dataset. But what if your DataFrame contains multiple columns?

For simplicity, let’s assume that you have the following dataset with 2 columns that contain NaN values:

values_1 values_2
700 NaN
NaN 150
500 NaN
NaN 400

You can then create the DataFrame as follows:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values_1': [700, np.nan, 500, np.nan],
                   'values_2': [np.nan, 150, np.nan, 400] 
                   })
print (df)

Run the code, and you’ll get the DataFrame with the two columns that include the NaNs:

   values_1  values_2
0     700.0       NaN
1       NaN     150.0
2     500.0       NaN
3       NaN     400.0

In order to replace the NaN values with zeros for the entire DataFrame using Pandas, you may use the third approach:

df.fillna(0)

For our example:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values_1': [700, np.nan, 500, np.nan],
                   'values_2': [np.nan, 150, np.nan, 400] 
                   })
df = df.fillna(0)

print (df)

You’ll now get 0’s, instead of all the NaNs, across the entire DataFrame:

   values_1  values_2
0     700.0       0.0
1       0.0     150.0
2     500.0       0.0
3       0.0     400.0

Case 4: replace NaN values with zeros for an entire DataFrame using NumPy

You can achieve the same goal for an entire DataFrame using NumPy:

df.replace(np.nan,0)

And for our example, you can apply the code below to replace the NaN values with zeros:

import pandas as pd
import numpy as np

df = pd.DataFrame({'values_1': [700, np.nan, 500, np.nan],
                   'values_2': [np.nan, 150, np.nan, 400] 
                   })
df= df.replace(np.nan,0)

print (df)

Run the code, and you’ll get the same results as in the previous case:

   values_1  values_2
0     700.0       0.0
1       0.0     150.0
2     500.0       0.0
3       0.0     400.0

You can find additional information about replacing values in Pandas DataFrame by visiting the Pandas documentation.

Alternatively, you may check this guide for the steps to drop rows with NaN values in Pandas DataFrame.