How to replace inf with zero in Pandas

In this post, we are going to learn about how to replace inf with zero in Pandas dataframe. The infinite values can be positive or negative and added in Pandas Dataframe by using the numpy library np.inf attribute. We can replace them using the dataframe replace() method in the whole dataframe, Replace inf in a specific column and replace inf based on the condition of dataframe

1. How to replace inf with zero in Pandas


The Pandas dataframe replace() method replace the existing value with given values in the Pandas dataframe. The dataframe.replace() method takes two arguments

  • First, the value we want to replace that is np. inf is can be positive or negative.
  • Second, the value with which the existing np. inf value will be replaced is 0.
  • The third argument is the inplace =True to make a change in the existing dataframe without creating a new copy.

In this Python program example, we will replace infinite(inf) values with zero. We have a dataframe that has inf values in some columns that are added by using the numpy library np. inf attribute. We have used the dataframe.replace() method to replace inf positive or negative values with zero in the whole dataframe.

import pandas as pd
import numpy as np
   
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.inf,98, -np.inf,100],
    'Subject': ['Math', 'Math', 'Math', np.inf]
}

dfobj = pd.DataFrame(data)

dfobj .replace([np.inf, -np.inf], 0, inplace=True)

print('Pandas Replaced  np.inf to zero:\n',dfobj )

Output

Pandas Replaced  np.inf to zero:
     Name  Marks Subject
0   Jack    0.0    Math
1   Rack   98.0    Math
2    Max    0.0    Math
3  David  100.0       0

Most Popular Post


2. How to replace inf with zero in Pandas column


In the above program, we have replaced infinite values with zero in the whole dataframe.To replace infinite value in dataframe specific column this syntax “dfobj[‘Marks’].replace([np.inf, -np.inf], 0, inplace=True)” is used and this will replace all negative and positive infinite value with zero in “Marks” column of Pandas dataframe.

import pandas as pd
import numpy as np
   
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.inf,98, -np.inf,100],
    'Subject': ['Math', 'Math', 'Math', np.inf]
}

dfobj = pd.DataFrame(data)


dfobj['Marks'].replace([np.inf, -np.inf], 0, inplace=True)

print('Pandas  column Replaced  np.inf with  zero:\n',dfobj )

Output

Pandas  column Replaced  np.inf with  zero    
Name  Marks Subject
0   Jack    0.0    Math
1   Rack   98.0    Math
2    Max    0.0    Math
3  David  100.0     inf

3. Pandas Replace Inf with zero-based on condition using np. where()


In this Python example, We will discuss how to replace inf with zero in Pandas column based on condition by using np. where().We have replaced the infinite value of “Marks” columns with zero.

import pandas as pd
import numpy as np
   
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.inf,98, np.inf,100],
    'Subject': ['Math', 'Math', 'Math', np.inf]
}

dfobj = pd.DataFrame(data)



dfobj['Marks'] = np.where((dfobj.Marks == np.inf),0,dfobj.Marks)
print('Pandas Replaced  np.inf with zero:\n',dfobj )

Output

Pandas Replaced  np.inf with zero:
     Name  Marks Subject
0   Jack    0.0    Math
1   Rack   98.0    Math
2    Max    0.0    Math
3  David  100.0     inf

4. Pandas Replace Inf with zero-based on condition using df. loc[]


In this Python program, we will learn how to replace inf with zero in Pandas column based on condition with the help of pandas dataframe loc[] method in which we have checked condition dfobj.Marks == np.inf and replaced “Marks” columns infinite values with zero.

import pandas as pd
import numpy as np
   
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.inf,98, np.inf,100],
    'Subject': ['Math', 'Math', 'Math', np.inf]
}

dfobj = pd.DataFrame(data)



dfobj.loc[(dfobj.Marks == np.inf),'Marks']=0
print('Pandas Replaced  np.inf with zero:\n',dfobj )

Output

Pandas Replaced  np.inf with zero:
     Name  Marks Subject
0   Jack    0.0    Math
1   Rack   98.0    Math
2    Max    0.0    Math
3  David  100.0     inf

Summary

In this post, we have learned how to replace inf with zero in Pandas with examples by using data fame. replace() method that includes Pandas replace inf with nan based on condition, Pandas replace inf with nan in a specific column of dataframe