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

Pandas dataframe might contain Nan values to denote the missing values.

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

Nan values are denoted using pd.Nat, np.NaN, None.

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.

If You’re in Hurry…

You can use the fillna() method to fill a value in place of the NaN values. You can pass any value that needs to be replaced for NaN.

To make the fill operation in the same dataframe instance, use the inplace=True parameter.

Code

df.fillna(0, inplace=True)

df

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

If You Want to Understand Details, Read on…

Different approaches are available to fill NaN values with Zero or any other value.

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

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

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

Using Replace

You can also use the replace() method to fill NaN values with Zero.

*The main difference method between fillna() and replace() is that the replace() method can be used to *replace any value* with some other value. Not only the NaN values.*

It accepts two mandatory parameters.

  • Value that needs to be replaced
  • Value that should be used for replacing

You can pass the inplace=True to make the replace operation 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, you can pass the subset of pandas dataframe columns as a list and invoke the fillna() method on specific columns.

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

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

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
6Trackpad0.0000.0NaNNaT

Replace NaN With Zero in a specific Column

To replace NaN with zero in a specific column, you can directly select the column using its name and invoke the fillna() method.

You can use the inplace=True parameter to make the replace operation in the same dataframe instead of creating a new dataframe object.

Code

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

df

The NaN values in the Unit_Price are replaced with zero.

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
6Trackpad0.000NaNNaNNaT

Replace NaN With Zero in All Columns

To replace the NaN with zero in all columns of the dataframe, you can 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.

You can use the fillna() method and pass the (df[‘column_name’].mean()) statement to replace the NaN value with 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

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

Dataframe Will Look Like

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

Conclusion

You’ve learned how to replace nan with zero in a column of Pandas dataframe. You’ve learned how to replace in a specific column, multiple columns at once and in all columns.

You also learned how to use the replace() method to replace any other value with an alternate value and how to fill the NaN value with the mean value.

If you’ve any questions, feel free to comment below.

You May Also Like

Leave a Comment