VOOZH about

URL: https://www.geeksforgeeks.org/python/replace-nan-values-with-zeros-in-pandas-dataframe/

⇱ Replace NaN Values with Zeros in Pandas DataFrame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace NaN Values with Zeros in Pandas DataFrame

Last Updated : 15 Jul, 2025

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. 

👁 Replace NaN Values with Zeros in Pandas DataFrame

Methods to Replace NaN Values with Zeros in Pandas DataFrame

In Python, there are two methods by which we can replace NaN values with zeros in Pandas dataframe. They are as follows:

Replace NaN Values with Zeros using Pandas fillna()

The fillna() function is used to fill NA/NaN values using the specified method. Let us see a few examples for a better understanding.

Replace NaN values with zeros for a column using Pandas fillna()

Syntax to replace NaN values with zeros of a single column in Pandas dataframe using fillna() function is as follows:

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

Output:

👁 Replace NaN values with zero for a single column using Panda fillna()
fillna() to replace NaN for a single column

Replace NaN values with zeros for an entire column using Pandas fillna()

Syntax to replace NaN values with zeros of the whole Pandas dataframe using fillna() function is as follows:

Syntax: df.fillna(0)

Output:

👁 Replace NaN values with zero for whole dataframe using Panda fillna()
fillna() function  to replace NaN for the whole dataframe

Replace NaN Values with Zeros using NumPy replace()

The dataframe.replace() function in Pandas can be defined as a simple method used to replace a string, regex, list, dictionary, etc. in a DataFrame.

Replace NaN values with zeros for a column using NumPy replace() 

Syntax to replace NaN values with zeros of a single column in Pandas dataframe using replace() function is as follows:

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

Output:

👁 Replace NaN values with zero for a single column using NumPy replace()
replace() to replace NaN for a single column

Replace NaN values with zeros for an entire Dataframe using NumPy replace() 

Syntax to replace NaN values with zeros of the whole Pandas dataframe using replace() function is as follows:

Syntax: df.replace(np.nan, 0)

Output:

👁 Replace NaN values with zero for whole dataframe using NumPy replace()
replace() function  to replace NaN for the whole dataframe
Comment