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 Overflow
🌐
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
🌐
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.
🌐
Medium
medium.com › @amit25173 › how-to-fill-nan-values-with-0-in-pandas-a665c5bf9967
How to Fill NaN Values with 0 in Pandas? | by Amit Yadav | Medium
March 6, 2025 - You don’t want your analysis to break just because of a few missing values, right? This is where fillna(0) comes in. It’s the easiest way to replace all NaN values with 0, ensuring your calculations stay accurate.
🌐
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)
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-fillna-to-replace-null-values-in-dataframe
Pandas DataFrame.fillna() | Python - GeeksforGeeks
February 23, 2026 - DataFrame.fillna() is used to replace missing values (NaN) in a Pandas DataFrame with a specified value or using a filling method. It helps clean incomplete data so that analysis and calculations can be performed correctly.
🌐
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
🌐
AskPython
askpython.com › home › how to replace nan values in a pandas dataframe with 0?
How to replace NaN values in a Pandas dataframe with 0? - AskPython
September 24, 2022 - In the above code, we applied the replace() function to replace NaN values with 0 in the ‘Rating’ column of the dataframe.
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily replace nan values with zero in pandas using fillna()
How To Easily Replace NaN Values With Zero In Pandas Using Fillna()
December 4, 2025 - The core function driving all these operations is fillna(). By setting the input parameter to 0, we instruct Pandas to replace all instances of NaN found within the specified selection with the integer or float representation of zero.
🌐
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.
🌐
datagy
datagy.io › home › pandas tutorials › missing data in pandas › pandas: replace nan with zeroes
Pandas: Replace NaN with Zeroes • datagy
December 15, 2022 - In the code above, we use the np.replace() method to replace all missing NaN values with the value 0. Similarly, we can use the NumPy .replace() method to replace NaN values with zeroes across an entire Pandas 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 - 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…