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 fillna: 

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

(2) For a single column using replace:

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

(3) For an entire DataFrame using fillna:

df.fillna(0, inplace=True)

(4) For an entire DataFrame using replace:

df.replace(np.nan, 0, inplace=True)

4 cases to replace NaN values with zeros in Pandas DataFrame

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

Suppose that you have a DataFrame in Python that contains columns with NaN values:

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)

If you run the code in Python, you’ll get the following DataFrame with the NaN values:

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

You can then use the following template to replace the NaN values with zeros for a single column in the DataFrame:

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

Let’s suppose that you wish to replace the NaN values with 0’s under the “values_1” column (but keep the NaNs under the second column). Here is the syntax that you may use:

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['values_1'] = df['values_1'].fillna(0)

print(df)

Run the code, and you’ll see that under the “values_1” column all the NaN values became zeros (but not under the second column):

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

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

You can accomplish the same task using replace:

df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 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['values_1'] = df['values_1'].replace(np.nan, 0)

print(df)

As before, the NaN values became zeros under the first column only:

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

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

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

df.fillna(0, inplace=True)

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.fillna(0, inplace=True)

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 replace

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

df.replace(np.nan, 0, inplace=True)

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.replace(np.nan, 0, inplace=True)

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.