How To Replace NaN With Zero in a Column Of Pandas Dataframe – With Examples

Nan values in the Pandas dataframe are denoted using pd.Nat, np.NaN, None.

You can replace nan with zero in a column of Pandas dataframe using the df.fillna(0, inplace=True) statement.

Basic Example

df.fillna(0, inplace=True)

df
  • Use the inplace=True parameter to fill in the same dataframe.

All the NaN values are replaced with Zeros.

Dataframe Will Look Like

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0001/05/2021
6Trackpad0.0000.000

This tutorial teaches you how to replace NaN with zero in a column or replace NaN in all columns using the fillna() or the replace() method.

Sample Dataframe

First, create a dataframe with missing values in it.

The sample dataframe contains missing values, and they are denoted using None, np.nan, and pd.NaT.

Code

import pandas as pd

import numpy as np

data = {
"product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers","Trackpad"],
 
"Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
 
"No_Of_Units":[5,5, 10, 20, 20, 8,np.nan],
 
"Available_Quantity":[5,6,10,"Not Available","Not Available", np.nan,np.nan],

"Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]

       }

df = pd.DataFrame(data)

df

Dataframe Will Look Like

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0NaN01/05/2021
6TrackpadNaNNaNNaNNaT

Using FillNa

You can use the fillna() method to replace the NaN value with any other value.

  • To fill the NaN value with Zero, pass 0 to the method
  • To make the changes in the same dataframe, instead of creating a new copy, use the inplace=True parameter
  • If you invoke the method directly on the dataframe object, the NaN values in all the columns and rows will be replaced with the specified value

Code

The following code demonstrates how to replace the NaN values with Zero in all columns and rows.

df.fillna(0, inplace=True)

df

Dataframe Will Look Like

The same dataframe object is modified, and the NaN value is replaced with 0.

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0001/05/2021
6Trackpad0.0000.000

Using Replace

The replace() method replaces the desired value with the specified value in the dataframe.

The main difference method between fillna() and replace() is that the

  • replace() method can be used to replace any value with a specified value. Not only the NaN values.
  • fillna() will only fill with NaN values

Pass the inplace=True to replace in the same dataframe instead of creating a new dataframe.

Code

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

df

The NaN values are replaced with zero in the same dataframe object.

Dataframe Will Look Like

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0001/05/2021
6Trackpad0.0000.000

Replace NaN With Zero in Mulitple Columns

To replace NaN with Zero in multiple columns instead of the complete dataframe,

It’ll replace the NaN values in that specific columns.

Since it is in the subset of columns, you cannot use the inplace=True parameter. Because of this, you need to assign the result to the same column list.

Code

The following code demonstrates how to replace NaN with Zero in multiple columns.

df[['Unit_Price','No_Of_Units']] = df[['Unit_Price','No_Of_Units']].fillna(0)

df

Dataframe Will Look Like

The NaN values in the specific columns are replaced with Zero.

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0NaN01/05/2021
6Trackpad0.0000.0NaNNaT

Replace NaN With Zero in a specific Column

To replace NaN with zero in a specific column,

  • Directly select the column using its name
  • Invoke the fillna() method.

Use the inplace=True parameter to replace in the same dataframe instead of creating a new dataframe object.

Code

df['Unit_Price'].fillna(0,  inplace=True)

df

Dataframe Will Look Like

The NaN values in the Unit_Price are replaced with zero.

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0NaN01/05/2021
6Trackpad0.000NaNNaNNaT

Replace NaN With Zero in All Columns

To replace the NaN with zero in all columns of the dataframe,

  • Invoke the fillna() method directly in the dataframe object.

Use the inplace=True parameter to replace NaNs in the same dataframe instead of creating a new object.

Code

The following code demonstrates how to replace NaN values with Zero in all columns of the dataframe.

df.fillna(0, inplace=True)

df

Dataframe Will Look Like

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.0511/5/2021
1Mouse200.0005.064/23/2021
2Monitor5000.23510.01008/21/2021
3CPU10000.55020.0Not Available09/18/2021
4CPU10000.55020.0Not Available09/18/2021
5Speakers250.5008.0001/05/2021
6Trackpad0.0000.000

Replace NaN With Mean

Sometimes, you may need to fill the NaN values with the mean value of the same column.

  • Use the fillna() method and pass the (df[‘column_name’].mean()) statement to replace the NaN value with the mean value of the column.

Code

The following code demonstrates how to fill Nan values of the No_Of_Units column with the mean value of the same column.

df['No_Of_Units'].fillna((df['No_Of_Units'].mean()), inplace=True)

df

Dataframe Will Look Like

The NaN value in the No_Of_Units is filled with the mean value of the column.

product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005.000000511/5/2021
1Mouse200.0005.00000064/23/2021
2Monitor5000.23510.0000001008/21/2021
3CPU10000.55020.000000Not Available09/18/2021
4CPU10000.55020.000000Not Available09/18/2021
5Speakers250.5008.000000NaN01/05/2021
6TrackpadNaN11.333333NaNNaT

Additional Resources

Leave a Comment