Pandas replace inf with nan

In this post, we are going to learn how to replace inf with nan Pandas datagram with examples by using Pandas dataframe replace() methods. The nan values are not a number or missing values. The inf values are infinite values that can be positive and negative.

1. Pandas replace inf with nan


The Pandas dataframe replace() method replace the existing value with given values in the Pandas dataframe.The dataframe.replace() method 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 np.nan
  • 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 np.nan. 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 np.nan 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], np.nan, inplace=True)

print('Replaced  inf to nan:\n',dfobj )

Output

Replaced  inf to nan:
     Name  Marks Subject
0   Jack    NaN    Math
1   Rack   98.0    Math
2    Max    NaN    Math
3  David  100.0     NaN

Most Popular Post


2. Pandas replace inf with nan dataframe column


In the above program, we have replaced infinite values with np.nan 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 np.nan 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], np.nan, inplace=True)

print('dataFrame column Replaced inf with  nan:\n',dfobj )

Output

dataFrame column Replaced inf with  nan:
     Name  Marks Subject
0   Jack    NaN    Math
1   Rack   98.0    Math
2    Max    NaN    Math
3  David  100.0     inf

3. Pandas Replace Inf with nan by condition using np. where()


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

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),np.nan,dfobj.Marks)
print('Pandas Replaced  Replaced inf with  nan:\n',dfobj )



Output

Pandas Replaced  Replaced inf with  nan:
     Name  Marks Subject
0   Jack    NaN    Math
1   Rack   98.0    Math
2    Max   -inf    Math
3  David  100.0     inf

4. Pandas Replace Inf with nan by condition using df. loc[]


In this python program how to replace inf with np.nan 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 np.nan.

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']=np.nan
print('Pandas Replaced  Replaced inf with  nan:\n',dfobj )



Output

Pandas Replaced  Replaced inf with  nan:
     Name  Marks Subject
0   Jack    NaN    Math
1   Rack   98.0    Math
2    Max   -inf    Math
3  David  100.0     inf

Summary

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