I think you need replace by dict:

cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].replace({'0':np.nan, 0:np.nan})
Answer from jezrael 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 pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 0, 15, 14, 19, 23, 25, 29], 'assists': [5, 0, 7, 0, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 0, 9, 0]}) #view DataFrame print(df) points assists rebounds 0 25 5 11 1 0 0 8 2 15 7 10 3 14 0 6 4 19 12 6 5 23 9 0 6 25 9 9 7 29 4 0 · We can use the following syntax to replace each zero in the DataFrame with a NaN value:
Discussions

Why should you replace a 0 with a NAN value?
Why should you replace a 0 with a NAN value · Learn Python and R for data science. Learn by coding and working with data in your browser. Build your portfolio with projects and become a data scientist · Why should you replace a 0 value with a Nan? When I compare the output from the object: ... More on community.dataquest.io
🌐 community.dataquest.io
0
0
January 20, 2021
Can't replace 0 to nan in Python using Pandas - Stack Overflow
I have dataframe with only 1 column. I want to replace all '0' to np.nan but I can't achieve that. More on stackoverflow.com
🌐 stackoverflow.com
[Pandas] Replacing Zero Values in a Column
First you can find the nonzero mean : nonzero_mean = df[ df.col != 0 ].mean() Then replace the zero values with this mean : df.loc[ df.col == 0, "col" ] = nonzero_mean More on reddit.com
🌐 r/learnpython
2
4
February 20, 2017
Pandas .fillna() replacing every value with NaN instead of replacing only NaN values.
You should look up the definition of inplace. Try to set it to false instead. (in place methods don't return anything -> None) More on reddit.com
🌐 r/learnpython
3
2
October 10, 2021
🌐
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).
🌐
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…
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.fillna.html
pandas.DataFrame.fillna — pandas 3.0.2 documentation
>>> 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 · >>> df2 = pd.DataFrame(np.zeros((4, 4)), columns=list("ABCE")) >>> df.fillna(df2) 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 NaN 3 0.0 3.0 0.0 4.0
🌐
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.
🌐
Arab Psychology
scales.arabpsychology.com › home › how can i replace all zero values in a pandas dataframe with nan?
How Can I Replace All Zero Values In A Pandas Dataframe With NaN?
June 26, 2024 - The process of replacing all zero values in a Pandas dataframe with NaN involves using the replace() function and specifying the value to be replaced as 0 and the replacement value as NaN.
🌐
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 - replace(np.nan, 0) is a more generic method that replaces any occurrence of NaN, even if it’s part of a different operation. ... While both methods will give the same result in most cases, fillna(0) is usually the preferred choice for handling ...
🌐
py4u
py4u.org › blog › python-pandas-replace-multiple-columns-zero-to-nan
Python Pandas: How to Replace Zero (0 or '0') with NaN in Multiple Columns – Fixing the 'astype(str).replace' Not Working Issue
Data cleaning is the backbone of reliable data analysis. One common task is replacing zeros (either numeric `0` or string `'0'`) with `NaN` (Not a Number), especially when zeros represent missing or invalid data (e.g., a sales value of `0` might indicate no data was recorded, not actual zero sales).
🌐
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 nan with 0 in a series using the replace() method, you first need to invoke the replace() method on the series. Here, we need to give numpy.nan value as the first input argument and 0 as the second input argument. After execution, the pandas replace method will return a series having ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-replace-zeros-with-previous-non-zero-value
Python Pandas: Replace Zeros with Previous Non-Zero Value - GeeksforGeeks
July 23, 2025 - One of the simplest ways to replace zeros with the previous non-zero value is to temporarily convert zeros to NaN (Not a Number), and then use the ffill() method to propagate the last valid observation forward.
🌐
GeeksforGeeks
geeksforgeeks.org › replace-all-the-nan-values-with-zeros-in-a-column-of-a-pandas-dataframe
Replace all the NaN values with Zero's in a column of a Pandas dataframe - GeeksforGeeks
August 25, 2021 - It is used to replace a regex, string,  list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame met ... Let’s discuss a program To change the values from a column that contains the values 'YES' and 'NO' with TRUE and FALSE.  First, Let's see a dataset. Code: Python3 # import pandas library import pandas as pd # load csv file df = pd.read_csv("supermarkets.csv") # show the dataframe df Output : For d ... Nan(Not a number) is a floating-point value which can't be converted into other data type expect to float.
🌐
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 ·
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › [pandas] replacing zero values in a column
r/learnpython on Reddit: [Pandas] Replacing Zero Values in a Column
February 20, 2017 -

Hi all,

I decided to take my first try at a kaggle competition, however, I've been struggling something for awhile now. Perhaps you can help.

Basically, I've got a dataframe where the latitude and longitude (floats) are both zero for a very very small number of lines.

The std deviation for these columns is tiny, so I was just going to replace the zero values with the mean values. How should I go about this? Nothing I have tried so far has worked.

Thanks.