How to Find infinity values in Pandas dataframe

In this post, we are going to learn how to Find infinity values in Pandas dataframe.To find infinite values in Pandas dataframe we have two built-in functions Isin() and np.isfinite(dataframe) function.We will find infinite values in the whole dataframe row and columns of the dataframe, an index of infinite values. To add infinite values in Pandas Dataframe numpy library np.inf for positive and -np.inf for negative infinite values is used. To run all programs make sure Numpy and Pandas libraries are installed on the System.

1. Find infinity values in Pandas dataframe


The dataframe.isin() method is used to filter the dataframe and check each element has given values and returns a dataframe of boolean. if given values are present then it returns true else it returns False.

In this Python program we are using isin([np.inf, -np.inf]) function to check if each dataframe elements contain positive(np.inf) and negative(-np.inf) infinite values and returning an boolean dataframe.We have counted the number of infinite the dataframe contain.

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.isin([np.inf, -np.inf])

print('infinte values in DataFrame:',dfobj)
count = np.isinf(dfobj).values.sum()
print("Total infinte values in Pandas Dataframe: " ,count)

Output

infinte values in DataFrame:    
 fee  Marks    Avg
0  False   True  False
1   True  False  False
2   True   True  False
3  False  False   True
Total infinte values in Pandas Dataframe:  5

2. Find infinity values in Pandas Dataframe


The numpy library isfinite() function is used to check if infinite values exist in the dataframe element-wise and return a boolean array. In this Python program, we have used np.isifinte () to check the infinite value in the whole dataframe by passing it as a parameter and it is returning a boolean array where False for infinite value and True for a finite 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)

infinite_val = np.isfinite(dfobj)


print("infinte values in DataFrame:\n " ,infinite_val)

Output

infinte values in DataFrame:
       fee  Marks    Avg
0   True  False   True
1  False   True   True
2  False  False   True
3   True   True  False

3. Pandas Find Row Index with infinite values


In this Python program we have dataframe.index() to find an index of rows that has infinite values and passed np.isinf(dfobj).any(1) passed as a parameter where isin() check for positive or negative infinite values and any() function return True if any element of dataframe return True else return False.

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) 
indexNum = dfobj.index[np.isinf(dfobj).any(1)]
 
print('Index of infint rows:\n',indexNum)

Output

Index of infint rows:
 Int64Index([0, 1, 2, 3], dtype='int64')

4. Pandas find Columns for infinite values


In this Python program code, we will understand how to find columns with infinite values in Pandas Dataframe We have used the following functions.

  • df.columns.to_series(): To select list of dataframe columns
  • np.isin() : Check if dataframe contain positive or negative infinite values
  • any() : function return True if any element of dataframe return True else return False
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)

colname = dfobj.columns.to_series()[np.isinf(dfobj).any()]
 
print('Column with infinte values:\n',colname)

Output

Column with infinte values:
 fee        fee
Marks    Marks
Avg        Avg
dtype: object

5. Count infinite Values in a Specific column of the DataFrame


In this Python program example, we have counted total infinite values in column “Marks” by using the sum() function

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)


inf_Col_count = np.isinf(df['Marks']).values.sum()


print("infinite value count in a column:\n " ,inf_Col_count)

Summary

In this post we have learn How to Find infinity values in Pandas dataframe with examples by using isin(),isfinite(),any(),sum() functions.