Pandas replace nan values with zero

Pandas-type-Conversion

In this post, we are going to learn How to replace nan values with zero in pandas dataframe with examples. We will use dataframe methods fillna() and replace(). The pandas dataframe fillna() method takes 0 as an argument to replace the NAN values with zero and returns a new dataframe in which NAN values are replaced by zero.

Whereas the dataframe replace() method takes two arguments the value to replace with which value to replace.

Pandas methods to replace nan values with zero


  • Replace():It can be used to replace ‘string’,’regx’,’dictionary’,’list’
  • fillna():It is used to replace NA/NAN

1. Pandas replace specfic column nan value with 0 using fillna()


In this example, We will discuss how to fill nan values with zero. To achieve this, first, we have to add nan values to pandas dataframe by using the numpy library that we have imported using “import numpy as np”.In which columns we want null values we have added using np. nan

  • The next step is to replace nan values with zero that is done by using fillna() method of Pandas dataframe
  • In this example, we are replacing the specific “Math” column nan value 0. So we have used the fillna() function with zero as an argument.
import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)
 
 
dfobj['Marks'] = dfobj['Marks'].fillna(0)
 
print (dfobj)
 
 


Output

   Name  Marks Subject
0  Jack  100.5     NaN
1  Rack    0.0    Math
2   NaN    0.0   Music

2. Pandas Replace all columns nan with 0 Using fillna()


In this python program example, we have multiple columns that have null values that we have added using NumPy library np. nan attribute. To replace the multiple columns nan value. We have called fillna() method with dataframe object.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)

#whole dataframe
df = dfobj.fillna(0)
 
print (df)

Output

  Name  Marks Subject
0  Jack  100.5       0
1  Rack    0.0    Math
2     0    0.0   Music

3. Pandas Replace nan with 0 by using replace() method


In this Python program. First, we have created a dataframe that has a null value in some columns. To add null value to the dataframe the numpy library np. nan attribute is used.

  • The NumPy library is imported using “import numpy as np”
  • To replace a specific column null value with zero,
  • we have called replace() method on the “Math” column.
    • First the value we want to replace that is np.nan
    • Second the value we want to replace with is 0.
import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)

dfobj['Marks'] = dfobj['Marks'].replace(np.nan, 0)
 
print (dfobj)

Output

   Name  Marks Subject
0  Jack  100.5     NaN
1  Rack    0.0    Math
2   NaN    0.0   Music

4. Pandas replace() method to Replace mutiple columns nan with 0


In this python program. First, we have created a dataframe that has a null value in some columns. To add null value to the dataframe the numpy library np. nan attribute is used.

  • The Python NumPy library is imported using “import numpy as np”.
  • To replace mutiple column null value with zero,
  • we have called replace() method with dataframe object and passed two agruement
    • First the value we want to replace that is np.nan
    • Second the value we want to replace with is 0.
import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)


#whole dataframe
df = dfobj.replace(np.nan, 0)
 
print (df)
 

Output

   Name  Marks Subject
0  Jack  100.5       0
1  Rack    0.0    Math
2     0    0.0   Music

Summary

In this post, we have learned Pandas replace nan values with zero. We have used the dataframe method fillna() and replace()