Remove infinite values from a given Pandas DataFrame
Last Updated :
26 Jul, 2020
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:
Similar Reads
Getting Unique values from a column in Pandas dataframe Let's see how can we retrieve the unique values from pandas dataframe. Let's create a dataframe from CSV file. We are using the past data of GDP from different countries. You can get the dataset from here. Python3 # import pandas as pd import pandas as pd gapminder_csv_url ='https://raw.githubuserco
2 min read
Getting Unique values from a column in Pandas dataframe Let's see how can we retrieve the unique values from pandas dataframe. Let's create a dataframe from CSV file. We are using the past data of GDP from different countries. You can get the dataset from here. Python3 # import pandas as pd import pandas as pd gapminder_csv_url ='https://raw.githubuserco
2 min read
How to Drop Negative Values in Pandas DataFrame Handling data effectively is crucial in data analysis and manipulation. One common task is cleaning the data by removing negative values, especially when they are considered outliers or invalid entries. The Pandas library in Python offers several efficient methods to accomplish this. This article wi
3 min read
Remove last n rows of a Pandas DataFrame Let's see the various methods to Remove last n rows of a Pandas Dataframe.First, let's make a dataframe: Python3 # Import Required Libraries import pandas as pd # Create a dictionary for the dataframe dict = { 'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel', 'Sanskriti', 'Abhishek Jain'], 'Age': [
3 min read
How to remove random symbols in a dataframe in Pandas? In this article, we will see how to remove random symbols in a dataframe in Pandas. Method 1: Selecting columns Syntax: dataframe[columns].replace({symbol:},regex=True) First, select the columns which have a symbol that needs to be removed. And inside the method replace() insert the symbol example r
2 min read
How to Drop Rows with NaN Values in Pandas DataFrame? In Pandas missing values are represented as NaN (Not a Number) which can lead to inaccurate analyses. One common approach to handling missing data is to drop rows containing NaN values using pandas. Below are some methods that can be used:Method 1: Using dropna()The dropna() method is the most strai
2 min read