Python Pandas – Check and Display row index with infinity

When working with Pandas DataFrames, you may need to identify rows containing infinity values. This is useful for data cleaning and analysis. Python provides np.isinf() and any() methods to check and display row indexes with infinity values.

Required Libraries

First, import the necessary libraries ?

import pandas as pd
import numpy as np

Creating DataFrame with Infinity Values

Create a DataFrame containing infinity values using np.inf ?

import pandas as pd
import numpy as np

# Create dictionary with infinity values
data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf]}

# Create DataFrame
dataFrame = pd.DataFrame(data)
print("DataFrame:")
print(dataFrame)
DataFrame:
   Reg_Price
0   7000.505700
1           inf
2   5000.000000
3           inf
4   9000.757680
5   6000.000000
6    900.000000
7           inf

Checking for Infinity Values

Count the total number of infinity values in the DataFrame ?

import pandas as pd
import numpy as np

data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf]}
dataFrame = pd.DataFrame(data)

# Count infinity values
count = np.isinf(dataFrame).values.sum()
print("Infinity values count:", count)
Infinity values count: 3

Getting Row Indexes with Infinity Values

Use np.isinf() with any(1) to get row indexes containing infinity values ?

import pandas as pd
import numpy as np

data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf]}
dataFrame = pd.DataFrame(data)

# Get row indexes with infinity values
indexNum = dataFrame.index[np.isinf(dataFrame).any(1)]
print("Row indexes with infinite values:")
print(indexNum)
Row indexes with infinite values:
Index([1, 3, 7], dtype='int64')

Complete Example

Here's the complete code demonstrating all operations ?

import pandas as pd
import numpy as np

# Create dictionary with infinity values
data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf]}

# Create DataFrame
dataFrame = pd.DataFrame(data)
print("DataFrame:")
print(dataFrame)

# Check for infinity values and display count
count = np.isinf(dataFrame).values.sum()
print("\nInfinity values count:", count)

# Get row indexes with infinity values
indexNum = dataFrame.index[np.isinf(dataFrame).any(1)]
print("\nRow indexes with infinite values:")
print(indexNum)
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

Row indexes with infinite values:
Index([1, 3, 7], dtype='int64')

How It Works

The process works in three steps:

  • np.isinf(dataFrame) returns a boolean DataFrame where True indicates infinity values
  • .any(1) checks if any value in each row is True (axis=1 means row-wise)
  • dataFrame.index[...] returns the indexes where the condition is True

Conclusion

Use np.isinf() with any(1) to identify rows containing infinity values in Pandas DataFrames. This method is essential for data validation and cleaning operations before analysis.

Updated on: 2026-03-26T02:56:57+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements