In this article we will discuss different ways to replace NaN Values with Zeros in a specific column of Dataframe or in complete DataFrame in Python.
Table Of Contents
- Replace NaN values with zero using fillna() method
- Replace NaN values with zero using replace() method
A DataFrame is a data structure that stores the data the in tabular format i.e. in the format of rows and columns. We can create a DataFrame using pandas.DataFrame() method. In Python , we can create NaN values using the numpy module.. Let’s use this to create a dataframe of four rows and five columns with few NaN values.
import pandas as pd import numpy as np # Create dataframe with 4 rows and 5 columns df= pd.DataFrame({'First' :[0, 0, 0, 0], 'Second' :[np.nan, np.nan,1 ,1], 'Third' :[0, 0, 0, 0], 'Fourth' :[0, 1, 89, np.nan], 'Fifth' :[34, np.nan,45,34]}) # Display the Dataframe print(df)
Output:
First Second Third Fourth Fifth 0 0 NaN 0 0.0 34.0 1 0 NaN 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0
Replace NaN values with zero using fillna()
In Pandas, both DataFrame and Series provides a member function fillna() to fill/replace NaN values with a specified value. Their Syntax are as follows,
Series.fillna(value)Â
It replaces all the NaN values in the calling Series object with the specified value
DataFrame.fillna(value)Â
It replaces all the NaN values in the calling DataFrame object with the specified value
Frequently Asked:
- Replace empty strings in a pandas DataFrame with NaN
- Replace NaN with preceding/previous values in Pandas
- Select Rows by column value in Pandas
- Pandas – Check if all values in a Column are Equal
Replace NaN values with zero in a column using fillna()
We can select a single column of Dataframe as a Series object and then call the fillna(0) on that column to replace all NaN values with zero in that column. For example,
import pandas as pd import numpy as np # Create dataframe with 4 rows and 5 columns df= pd.DataFrame({'First' :[0, 0, 0, 0], 'Second' :[np.nan, np.nan,1 ,1], 'Third' :[0, 0, 0, 0], 'Fourth' :[0, 1, 89, np.nan], 'Fifth' :[34, np.nan,45,34]}) # Display the Dataframe print(df) # Replace NaN with zero on column 'Second' df['Second'] = df['Second'].fillna(0) # Display the Dataframe print(df)
Output:
First Second Third Fourth Fifth 0 0 NaN 0 0.0 34.0 1 0 NaN 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0 First Second Third Fourth Fifth 0 0 0.0 0 0.0 34.0 1 0 0.0 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0
Here, we selected the column ‘Second’ as a Series object and then called the fillna() function on that with the parameter value 0. Therefore, it replaced all the NaN values in column ‘Second’ with zero.
Replace NaN Values with Zeros entire dataframe using fillna()
Call the fillna() function of the DataFrame object with parameter value 0. It will replace NaN values in entire DataFrame with zero. For example,
import pandas as pd import numpy as np # Create dataframe with 4 rows and 5 columns df= pd.DataFrame({'First' :[0, 0, 0, 0], 'Second' :[np.nan, np.nan,1 ,1], 'Third' :[0, 0, 0, 0], 'Fourth' :[0, 1, 89, np.nan], 'Fifth' :[34, np.nan,45,34]}) # Display the Dataframe print(df) # Replace NaN with zero in entire DataFrame df = df.fillna(0) # Display the Dataframe print(df)
Output:
First Second Third Fourth Fifth 0 0 NaN 0 0.0 34.0 1 0 NaN 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0 First Second Third Fourth Fifth 0 0 0.0 0 0.0 34.0 1 0 0.0 0 1.0 0.0 2 0 1.0 0 89.0 45.0 3 0 1.0 0 0.0 34.0
Replace NaN values with zero using replace()
In Pandas, both the Dataframe and series class provides a function replace() to change the contents. We are going to use the se functions,
DataFrame.replace()
To replace all the occurrences of a value in entire Dataframe, pass the item to be replaced and replacement value as arguments in it.
DataFrame.replace(to_replace, value)
Series.replace()
Series.replace(to_replace, value)
To replace the value to be changed with the given value.
Let’s use this to replace NaN values with zero.
Replace NaN Values with Zeros in a column using replace()
Select the column ‘Second’ as a Series object from the Dataframe and call the replace() function to replace all NaN values in that column with zero. For example,
import pandas as pd import numpy as np # Create dataframe with 4 rows and 5 columns df= pd.DataFrame({'First' :[0, 0, 0, 0], 'Second' :[np.nan, np.nan,1 ,1], 'Third' :[0, 0, 0, 0], 'Fourth' :[0, 1, 89, np.nan], 'Fifth' :[34, np.nan,45,34]}) # Display the Dataframe print(df) # Replace NaN with zero in column 'Second' df['Second'] = df['Second'].replace(np.NaN, 0) # Display the Dataframe print(df)
Output:
First Second Third Fourth Fifth 0 0 NaN 0 0.0 34.0 1 0 NaN 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0 First Second Third Fourth Fifth 0 0 0.0 0 0.0 34.0 1 0 0.0 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0
Replace NaN Values with Zeros in entire dataframe using replace()
Call the replace() function on DataFrame object with arguments NaN and 0. It will replace all occurrences of NaNs with zero in the entire DataFrame. For example,
import pandas as pd import numpy as np # Create dataframe with 4 rows and 5 columns df= pd.DataFrame({'First' :[0, 0, 0, 0], 'Second' :[np.nan, np.nan,1 ,1], 'Third' :[0, 0, 0, 0], 'Fourth' :[0, 1, 89, np.nan], 'Fifth' :[34, np.nan,45,34]}) # Display the Dataframe print(df) # Replace NaN with zero in entore DataFrame df = df.replace(np.NaN, 0) # Display the Dataframe print(df)
Output:
First Second Third Fourth Fifth 0 0 NaN 0 0.0 34.0 1 0 NaN 0 1.0 NaN 2 0 1.0 0 89.0 45.0 3 0 1.0 0 NaN 34.0 First Second Third Fourth Fifth 0 0 0.0 0 0.0 34.0 1 0 0.0 0 1.0 0.0 2 0 1.0 0 89.0 45.0 3 0 1.0 0 0.0 34.0
Summary
In this article we learned about two different ways to replace NaN values with Zeros either in a column or in entire dataframe.