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 OverflowStatology
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:
Top answer 1 of 7
122
I think you need replace by dict:
cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].replace({'0':np.nan, 0:np.nan})
2 of 7
10
You could use the 'replace' method and pass the values that you want to replace in a list as the first parameter along with the desired one as the second parameter:
cols = ["Weight","Height","BootSize","SuitSize","Type"]
df2[cols] = df2[cols].replace(['0', 0], np.nan)
Videos
04:00
Replace NaN with 0 in pandas DataFrame in Python (2 Examples) | ...
01:41
Replacing NaN values with zero in pandas DataFrame in Python - YouTube
04:43
Pandas Change NaN to Zeros in DataFrame - YouTube
Replace Blank Values by NaN in pandas DataFrame in ...
02:43
How to Replace Blank Values with 0 in a Pandas DataFrame - YouTube
01:52
How to Quickly Convert NaN Values in Pandas to 0 and Non-zero Values ...
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)
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) ...
GeeksforGeeks
geeksforgeeks.org โบ python โบ replace-nan-values-with-zeros-in-pandas-dataframe
Replace NaN Values with Zeros in Pandas DataFrame - GeeksforGeeks
Replace NaN values with zeros for a column using Pandas fillna()
Published ย July 15, 2025
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.
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.