Home »
Python »
Python Programs
Pandas: How to remove nan and -inf values?
Learn, how to remove nan and -inf values in Python Pandas?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose that we are given a dataframe that contains several rows and columns with nan and -inf values too. We need to remove these nans and -inf values for better data analysis.
Removing nan and -inf values
For this purpose, we will use pandas.DataFrame.isin() and check for rows that have any with pandas.DataFrame.any(). Finally, we will use the boolean array to slice the dataframe.
Let us understand with the help of an example,
Python program to remove nan and -inf values from pandas dataframe
# Importing pandas package
import pandas as pd
# Import numpy
import numpy as np
from numpy import inf
# Creating a dataframe
df = pd.DataFrame(data={'X': [1,1,np.nan], 'Y': [8,-inf,7], 'Z': [5,-inf,4],'A': [3,np.nan,7]})
# Display the DataFrame
print("Original DataFrame:\n",df,"\n\n")
# Removing nan and -inf
res = df[~df.isin([np.nan, -np.inf]).any(1)]
# Display Result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »