You can set inplace to True (default is False):
area.replace(0, np.nan, inplace=True)
See examples in docs.
Answer from zipa on Stack OverflowStatology
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
Videos
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 ...
04:43
Pandas Change NaN to Zeros in DataFrame - YouTube
01:41
Replacing NaN values with zero in pandas DataFrame in Python - YouTube
02:43
How to Replace Blank Values with 0 in a Pandas DataFrame - YouTube
GeeksforGeeks
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 the whole Pandas dataframe using fillna() function is as follows: Syntax: df.fillna(0) Python ·
Published July 15, 2025
PythonForBeginners.com
pythonforbeginners.com › home › pandas replace nan with 0 in dataframe
Pandas Replace NaN With 0 in Dataframe - PythonForBeginners.com
January 17, 2023 - To replace the nan value with 0 in a series using the apply() method, we will invoke the apply() method on the series and pass the function as an input argument. After execution of the apply() method, we will get the modified series as shown below. import pandas as pd import numpy as np def ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.fillna.html
pandas.DataFrame.fillna — pandas 3.0.2 documentation
>>> df.fillna(0) A B C D 0 0.0 2.0 0.0 0.0 1 3.0 4.0 0.0 1.0 2 0.0 0.0 0.0 0.0 3 0.0 3.0 0.0 4.0 · Replace all NaN elements in column ‘A, ‘B’, ‘C’, and ‘D’, with 0, 1, 2, and 3 respectively.
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)
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)
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas replace nan values with zero in a column
Pandas Replace NaN Values with Zero in a Column - Spark By {Examples}
June 26, 2025 - You can use the pandas.DataFrame.fillna() or pandas.DataFrame.replace() methods to replace all NaN or None values in an entire DataFrame with zeros (0). NaN, which stands for Not A Number, is a common representation for missing values in data. Sometimes None is also used to represent missing values.
Erikrood
erikrood.com › Python_References › replace_nan_zero_final.html
Replace all NaN values with 0's in a column of Pandas dataframe
Practice interviewing with a few questions per week. 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 ·
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.
Statology
statology.org › home › how to replace nan values with zero in pandas
How to Replace NaN Values with Zero in Pandas
September 18, 2021 - #replace NaN values with zero in 'assists' column df['assists'] = df['assists'].fillna(0) #view updated DataFrame print(df) points assists rebounds 0 25.0 5.0 11.0 1 NaN 0.0 8.0 2 15.0 7.0 10.0 3 14.0 0.0 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
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.
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 - Left unchecked, NaNs can stop you from running simple operations like sums or averages, leading to errors or misleading insights. That’s why learning how to handle them is a crucial step for anyone working with data. Let’s get started! The quickest way to replace missing values in pandas is with fillna(0...
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-32.php
Pandas: Replace all the NaN values with Zero's in a column of a dataframe - w3resource
September 5, 2025 - Write a Pandas program to fill NaN values with zero across multiple columns using the fillna() method. Write a Pandas program to update a DataFrame column by replacing all NaN entries with zero and then plot a histogram of the column.
Dataquest Community
community.dataquest.io › q&a › dq courses
Why should you replace a 0 with a NAN value? - DQ Courses - Dataquest Community
January 20, 2021 - Screen Link: My Code: import numpy as np prev_rank_before = f500["previous_rank"].value_counts(dropna=False).head() f500.loc[f500["previous_rank"] == 0, "previous_rank"] = np.nan prev_rank_after = f500["previous_ran…