DataFrame.fillna() or Series.fillna() will do this for you.
Example:
In [7]: df
Out[7]:
0 1
0 NaN NaN
1 -0.494375 0.570994
2 NaN NaN
3 1.876360 -0.229738
4 NaN NaN
In [8]: df.fillna(0)
Out[8]:
0 1
0 0.000000 0.000000
1 -0.494375 0.570994
2 0.000000 0.000000
3 1.876360 -0.229738
4 0.000000 0.000000
To fill the NaNs in only one column, select just that column.
In [12]: df[1] = df[1].fillna(0)
In [13]: df
Out[13]:
0 1
0 NaN 0.000000
1 -0.494375 0.570994
2 NaN 0.000000
3 1.876360 -0.229738
4 NaN 0.000000
Or you can use the built in column-specific functionality:
df = df.fillna({1: 0})
Answer from Aman on Stack OverflowGeeksforGeeks
geeksforgeeks.org › python › replace-nan-values-with-zeros-in-pandas-dataframe
Replace NaN Values with Zeros in Pandas DataFrame - GeeksforGeeks
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)
Published July 15, 2025
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.fillna.html
pandas.DataFrame.fillna — pandas 3.0.2 documentation
Replace all NaN elements in column ‘A, ‘B’, ‘C’, and ‘D’, with 0, 1, 2, and 3 respectively. >>> values = {"A": 0, "B": 1, "C": 2, "D": 3} >>> df.fillna(value=values) A B C D 0 0.0 2.0 2.0 0.0 1 3.0 4.0 2.0 1.0 2 0.0 1.0 2.0 3.0 3 0.0 3.0 2.0 4.0 · Only replace the first NaN element. >>> df.fillna(value=values, limit=1) A B C D 0 0.0 2.0 2.0 0.0 1 3.0 4.0 NaN 1.0 2 NaN 1.0 NaN 3.0 3 NaN 3.0 NaN 4.0 · When filling using a DataFrame, replacement happens along the same column names and same indices
Videos
01:41
Replacing NaN values with zero in pandas DataFrame in Python - YouTube
04:43
Pandas Change NaN to Zeros in DataFrame - YouTube
04:00
Replace NaN with 0 in pandas DataFrame in Python (2 Examples) | ...
14:16
Pandas Replace Dash with NaN - YouTube
Replace Blank Values by NaN in pandas DataFrame in ...
02:43
How to Replace Blank Values with 0 in a Pandas DataFrame - YouTube
Top answer 1 of 16
1020
DataFrame.fillna() or Series.fillna() will do this for you.
Example:
In [7]: df
Out[7]:
0 1
0 NaN NaN
1 -0.494375 0.570994
2 NaN NaN
3 1.876360 -0.229738
4 NaN NaN
In [8]: df.fillna(0)
Out[8]:
0 1
0 0.000000 0.000000
1 -0.494375 0.570994
2 0.000000 0.000000
3 1.876360 -0.229738
4 0.000000 0.000000
To fill the NaNs in only one column, select just that column.
In [12]: df[1] = df[1].fillna(0)
In [13]: df
Out[13]:
0 1
0 NaN 0.000000
1 -0.494375 0.570994
2 NaN 0.000000
3 1.876360 -0.229738
4 NaN 0.000000
Or you can use the built in column-specific functionality:
df = df.fillna({1: 0})
2 of 16
200
It is not guaranteed that the slicing returns a view or a copy. You can do
df['column'] = df['column'].fillna(value)
InterviewQs
interviewqs.com › ddi-code-snippets › nan-replace-zero
Replace all NaN values with 0's in a column of Pandas dataframe - InterviewQs
A step-by-step Python code example that shows how to replace all NaN values with 0's in a column of Pandas DataFrame. Provided by InterviewQs, a mailing list for coding and data interview problems.
Erikrood
erikrood.com › Python_References › replace_nan_zero_final.html
Replace all NaN values with 0's in a column of Pandas dataframe
import pandas as pd import numpy as np · raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'], 'age': [20, 19, 22, 21], 'favorite_color': ['blue', 'red', 'yellow', "green"], 'grade': [88, 92, 95, 70]} df = pd.DataFrame(raw_data, index = ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel']) df · #First, we have to create the NaN values df = df.replace(20,np.NaN) df = df.replace(70,np.NaN) df ·
Statology
statology.org › home › pandas: how to replace zero with nan
Pandas: How to Replace Zero with NaN
October 3, 2022 - import numpy as np #replace all zeros with NaN values df.replace(0, np.nan, inplace=True) #view updated DataFrame print(df) points assists rebounds 0 25.0 5.0 11.0 1 NaN NaN 8.0 2 15.0 7.0 10.0 3 14.0 NaN 6.0 4 19.0 12.0 6.0 5 23.0 9.0 NaN 6 25.0 9.0 9.0 7 29.0 4.0 NaN · Notice that each zero in every column of the DataFrame has been replaced with NaN. Note: We must use the argument inplace=True or else the changes won’t be made to the original DataFrame. Related: How to Replace NaN Values with Zero in Pandas
PythonForBeginners.com
pythonforbeginners.com › home › pandas replace nan with 0 in dataframe
Pandas Replace NaN With 0 in Dataframe - PythonForBeginners.com
January 17, 2023 - For this, we have first selected the column using the python indexing operator. Then, we have used the fillna() method to replace null values with 0 in the column. Instead of a single column, you can replace nan with 0 in multiple columns of pandas dataframe using the fillna() method as shown ...
Python Guides
pythonguides.com › pandas-replace-nan-with-0
How To Fill NaN Values With Zeros In Pandas DataFrames
May 21, 2025 - Read Fix “Function Not Implemented for This Dtype” Error in Python · The simplest and most direct way to replace NaN values with zeros is to use the fillna() method in Python: # Replace all NaN values with 0 merged_sales_filled = merged_sales.fillna(0) print(merged_sales_filled)
Python Examples
pythonexamples.org › pandas-dataframe-replace-nan-values-with-zero
How to Replace NaN values with Zero in Pandas DataFrame?
You can replace NaN values with 0 in Pandas DataFrame using DataFrame.fillna() method. Pass zero as argument to fillna() method and call this method on the DataFrame in which you would like to replace NaN values with zero.
TutorialsPoint
tutorialspoint.com › article › python-pandas-replace-all-nan-elements-in-a-dataframe-with-0s
Python Pandas - Replace all NaN elements in a DataFrame with 0s
2 weeks ago - Use fillna(0) to replace all NaN values with zeros in a DataFrame.
Syntx Scenarios
syntaxscenarios.com › home › python › replace nan values with zeros in pandas dataframe
Replace NaN Values with Zeros in Pandas DataFrame - Syntax Scenarios
October 3, 2025 - The quickest way to replace missing values in pandas is with fillna(0). Think of it as telling pandas: “Whenever you see a blank, write zero instead.” · import pandas as pd import numpy as np # Sample DataFrame df = pd.DataFrame({ "Product": ...
Altcademy
altcademy.com › blog › how-to-replace-nan-with-0-in-pandas
How to replace nan with 0 in Pandas - Altcademy.com
January 12, 2024 - # Replace NaN values with 0 in 'Column1' only df['Column1'] = df['Column1'].fillna(0) # Check the DataFrame now print(df) Now, only the NaN values in 'Column1' have been replaced with zeros, leaving the other columns untouched. What if you want to be a bit more sophisticated with your replacements? For instance, maybe you want to fill in the average sales for days with missing data, rather than just a zero. Pandas has got you covered:
Aporia
aporia.com › home › how to replace nan values by zeros in a dataframe
Replace NaN Values with Zeros in Pandas or Pyspark DataFrame
September 4, 2024 - In this how-to article, we will learn how to replace NaN values by zeros in Pandas and PySpark DataFrames. The fillna function can be used for replacing missing values. We just need to write the value to be used as the replacement inside the function. # Replace all missing values in the DataFrame df = df.fillna(0...
IncludeHelp
includehelp.com › python › how-to-replace-the-nan-values-with-zeros-in-pandas-dataframe.aspx
How to Replace NaN Values with Zeros in Pandas DataFrame?
To replace NaN values with zeroes in a Pandas DataFrame, you can simply use the DataFrame.replace() method by passing two parameters to_replace as np.NaN and value as 0.