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:
๐ŸŒ
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โ€ฆ
๐ŸŒ
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
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.
๐ŸŒ
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
๐ŸŒ
Python Examples
pythonexamples.org โ€บ pandas-dataframe-replace-nan-values-with-zero
How to Replace NaN values with Zero in Pandas DataFrame?
Then we will use fillna() method to replace these numpy.nan values with zero. import pandas as pd import numpy as np df = pd.DataFrame( [[np.nan, 72, 67], [23, 78, 62], [32, 74, np.nan], [np.nan, 54, 76]], columns=['a', 'b', 'c']) print('Original DataFrame\n', df) df = df.fillna(0) ...
Find elsewhere
๐ŸŒ
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).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-replace-nan-values-by-zeroes-in-a-column-of-a-pandas-series
Python Pandas - Replace all NaN elements in a DataFrame with 0s
August 30, 2021 - To replace NaN values, use the fillna() method. Letโ€™s say the following is our CSV file opened in Microsoft Excel with some NaN values โˆ’ ... import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv...
๐ŸŒ
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 โ€บ 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.
๐ŸŒ
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.
๐ŸŒ
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 ยท
๐ŸŒ
TidyStat
tidystat.com โ€บ home โ€บ how to replace nan with zero in pandas
How to Replace NaN with Zero in Pandas - TidyStat -
January 20, 2025 - You can replace NaN with zero using either fillna(0) in Pandas or replace(np.nan,0) in Numpy.