Pandas replace inf with max value of column

In this post, We will understand how to replace inf with the max value of the column. The inf value is an infinite value which is a positive or negative value that is represented by np.inf or -np.inf.The Pandas dataframe replaces() method is used to do this job.

1. Pandas replace inf with max value in Dataframe


In this python program, We are using the isfinite() method to check value is infinite in the dataframe. if it is infinite replace the positive infinite value with the max value using the max() method and the negative infinite value with the minimum value throughout the dataframe.

import pandas as pd
import numpy as np
   
data = {
    'fee': [100, np.inf, np.inf, 100],
    'Marks':[np.inf,98, -np.inf,100],
    'Avg': [100,98,100, np.inf]
}

dfobj = pd.DataFrame(data)

dfobj = dfobj.replace({np.inf: dfobj[np.isfinite(dfobj)].max().max(), 
                -np.inf: dfobj[np.isfinite(dfobj)].min().min()})

print('Replaced  inf with max:\n',dfobj)

Output

Replaced inf with max :
      fee  Marks    Avg
0  100.0  100.0  100.0
1  100.0   98.0   98.0
2  100.0   98.0  100.0
3  100.0  100.0  100.0

2. Pandas replace inf with max value in Dataframe


In this Python program example, we are using dfobj.eq() method to check positive and negative infinite values in values in dataframe and replace infinite positive max value and negative infinite value with minimum value.

import pandas as pd
import numpy as np
   
data = {
    'Fee': [100, np.inf, np.inf, 100],
    'Marks':[np.inf,98, -np.inf,100],
    'Avg': [100,98,100,np.inf]
}

dfobj = pd.DataFrame(data)

pos_inf = dfobj.eq(np.inf)
neg_inf = dfobj.eq(-np.inf)


dfobj = dfobj.mask(pos_inf, dfobj[~pos_inf].max().max()).mask(neg_inf, dfobj[~neg_inf].min().min())

print('Replaced  inf  with max:\n',dfobj )

Output

Replaced  inf  with max:
      Fee  Marks    Avg
0  100.0  100.0  100.0
1  100.0   98.0   98.0
2  100.0   98.0  100.0
3  100.0  100.0  100.0

3. Pandas replace inf with max value of column


In this Python program example we are using df.loc[] method finding the max value in Pandas dataframe. The Pandas dataframe replace() method to replace [np.inf,-np.inf] with max values and inplace = True to change in the existing dataframe without creating a copy of dataframe.

import pandas as pd
import numpy as np
   
data = {
    'fee': [100, np.inf, np.inf, 100],
    'Marks':[np.inf,98, -np.inf,100],
    'Avg': [100,98,100,np.inf]
}

dfobj = pd.DataFrame(data)


max_val = dfobj.loc[dfobj['Marks'] != np.inf, 'Marks'].max()

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

print('Replaced  inf  with max :\n',dfobj )

Output

Replaced  inf  with max :
      fee  Marks    Avg
0  100.0  100.0  100.0
1    inf   98.0   98.0
2    inf  100.0  100.0
3  100.0  100.0    inf

4. Pandas replace inf with a max value of column


In this python program example, We have used the loc[] method to replace the infinite positive value with the max value in a column of the dataframe “Math” column.

import pandas as pd
import numpy as np
   
data = {
    'Fee': [100, np.inf, np.inf, 100],
    'Marks':[np.inf,98, -np.inf,100],
    'Avg': [100,98,100,np.inf]
}

dfobj = pd.DataFrame(data)
mask = dfobj['Marks'] != np.inf
dfobj.loc[~mask, 'Marks'] = dfobj.loc[mask, 'Marks'].max()
print('Replaced  inf  with max:\n',dfobj )

Output

Replaced  inf  with max:
      Fee  Marks    Avg
0  100.0  100.0  100.0
1    inf   98.0   98.0
2    inf   -inf  100.0
3  100.0  100.0    inf

Summary

In this post We have learned Pandas replace inf with max value of column with examples by using the dataframe.replace() method for entire dataframe and column of dataframe