Python Pandas – Check and Display row index with infinity



To check and display row index, use the isinf() with any(). At first, let us import the required libraries with their respective aliases −

import pandas as pd import numpy as np

Create a dictionary of list. We have set the infinity values using the Numpy np.inf

d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } 

Creating DataFrame from the above dictionary of list −

dataFrame = pd.DataFrame(d)

Getting row index with infinity values −

indexNum = dataFrame.index[np.isinf(dataFrame).any(1)] 

Example

Following is the code −

import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } # creating dataframe from the above dictionary of list dataFrame = pd.DataFrame(d) print"DataFrame...\n",dataFrame # checking for infinite values and displaying the count count = np.isinf(dataFrame).values.sum() print"\nInfinity values count...\n ",count # getting row index with infinity values indexNum = dataFrame.index[np.isinf(dataFrame).any(1)] print"\nDisplay row indexes with infinite values...\n ",indexNum

Output

This will produce the following output −

DataFrame...     Reg_Price 0 7000.505700 1         inf 2 5000.000000 3         inf 4 9000.757680 5 6000.000000 6  900.000000 7       inf Infinity values count... 3 Display row indexes with infinite values... Int64Index([1, 3, 7], dtype='int64')
Updated on: 2021-09-20T12:30:07+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements