
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Check if Pandas dataframe contains infinity
To check, use the isinf() method. To find the count of infinite values, use sum(). 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] }
Creating dataframe from the above dictionary of list
dataFrame = pd.DataFrame(d)
Checking for infinite values using isinf() and displaying the count
count = np.isinf(dataFrame).values.sum()
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] } # 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...\n ",count
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 Infinity values... 2
- Related Articles
- Python Pandas - Check if the dataframe objects are equal or not
- Python Pandas – Check and Display row index with infinity
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not
- How to check if any value is NaN in a Pandas DataFrame?
- Check if list contains consecutive numbers in Python
- Python – Merge two Pandas DataFrame
- Python Pandas - Check if the Pandas Index holds Interval objects
- Check if list contains all unique elements in Python
- Python Pandas - Check if an interval is empty
- Python Pandas - Check if the index has NaNs
- Python - Grouping columns in Pandas Dataframe
- Python - Filter Pandas DataFrame with numpy
- Python Pandas - Create Multiindex from dataframe
- Python - Filter Pandas DataFrame by Time
- Python - Ranking Rows of Pandas DataFrame

Advertisements