Let’s discuss how to Remove the infinite values from the Pandas dataframe. First let’s make a dataframe:
Example:
Python3
# Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = { 'Name' : [ 'Sumit Tyagi' , 'Sukritin' , 'Akriti Goel' , 'Sanskriti' , 'Abhishek Jain' ], 'Age' : [ 22 , 20 , np.inf, - np.inf, 22 ], 'Marks' : [ 90 , 84 , 33 , 87 , 82 ]} # Converting Dictionary to Pandas Dataframe df = pd.DataFrame( dict ) # Print Dataframe df |
Output:
Method 1: Replacing infinite with Nan and then dropping rows with Nan
We will first replace the infinite values with the NaN values and then use the dropna() method to remove the rows with infinite values. df.replace() method takes 2 positional arguments. First is the list of values you want to replace and second with which value you want to replace the values.
Python3
# Replacing infinite with nan df.replace([np.inf, - np.inf], np.nan, inplace = True ) # Dropping all the rows with nan values df.dropna(inplace = True ) # Printing df df |
Output:
Method 2: Changing Pandas option to consider infinite as Nan
Pandas provide the option to use infinite as Nan. It makes the whole pandas module to consider the infinite values as nan. We can do this by using pd.set_option(). It sets the option globally throughout the complete Jupyter Notebook.
Syntax:
pd.set_option('mode.use_inf_as_na', True)
It sets the options to use infinite as a Nan value throughout the session or until the options are not set back to the False.
Python3
# Changing option to use infinite as nan pd.set_option( 'mode.use_inf_as_na' , True ) # Dropping all the rows with nan values df.dropna(inplace = True ) # Printing df df |
Output:
Method 3: Consider infinite as Nan but using option_context
Instead of using pd.set_options(), which sets the option globally, we can use pd.option_context(), which changes option within the certain scope only.
Python3
# Changing option to use infinite as nan with pd.option_context( 'mode.use_inf_as_na' , True ): # Dropping the rows with nan # (or inf) values df.dropna(inplace = True ) # Printing df df |
Output:
Method 4: Using the filter
We will first create a filter which returns a boolean dataframe and use this filter to mask the infinite values.
Python3
# Creating filter df_filter = df.isin([np.nan, np.inf, - np.inf]) # Masking df with the filter df = df[~df_filter] # Dropping rows with nan values df.dropna(inplace = True ) # Printing df df |
Output: