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})
Answer from Aman on Stack OverflowGeeksforGeeks
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 a single column in Pandas dataframe using fillna() function is as follows: Syntax: df['DataFrame Column'] = df['DataFrame Column'].fillna(0)
Published July 15, 2025
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.fillna.html
pandas.DataFrame.fillna — pandas 3.0.2 documentation
Replace all NaN elements in column ‘A, ‘B’, ‘C’, and ‘D’, with 0, 1, 2, and 3 respectively.
Videos
04:43
Pandas Change NaN to Zeros in DataFrame - YouTube
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
14:16
Pandas Replace Dash with NaN - YouTube
Replace Blank Values by NaN in pandas DataFrame in ...
02:43
How to Replace Blank Values with 0 in a Pandas DataFrame - YouTube
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)
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 - Some of the students have missing scores, which are represented as np.nan values. df = df.fillna(0): The fillna() method is then used to fill in these missing values with 0. Finally the resulting DataFrame is printed to the console using print() function. ... Write a Pandas program to replace NaN values with zeros in a specified column and then check data types remain unchanged.
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)
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
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 ·
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) ...
TidyStat
35.199.173.34.bc.googleusercontent.com › home › how to replace nan with zero in pandas
How to Replace NaN with Zero in Pandas
January 20, 2025 - You can replace NaN with zero using either fillna(0) in Pandas or replace(np.nan,0) in Numpy. Single Column: Method 1: df['Column_name'].fillna(0) Method 2: df['Column_name'].replace(np.nan,0) Whole dataframe: Method 1: df.fillna(0) Method 2: df.replace(np.nan,0) Example 1: single column The ...
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…
Arab Psychology
scales.arabpsychology.com › home › how to easily replace nan values with strings in a pandas dataframe
How To Easily Replace NaN Values With Strings In A Pandas DataFrame
December 3, 2025 - Observation confirms that the NaN at index 0 of the ‘points’ column has been converted to ‘zero’. Significantly, the NaNs in ‘assists’ and ‘rebounds’ (at indices 1, 3, and 7) remain unchanged, demonstrating the surgical precision of applying the DataFrame.fillna() method to a single ...