Remove nan From List In Python?

Remove nan from list in Python

In python, nan is a special value used to represent missing values. In this article, we will discuss the properties of nan values. We will also look at different ways how we can remove nan values from list in python.

What is nan in python?

The nan values are special floating-point numbers defined in the math module and the numpy module in python. They are normally used to represent missing values in a given data.  

You can import and use nan values from the math module as follows.

Output:

Alternatively, you can import nan values from the numpy module as follows.

Output:

The nan values have a special property that two nan values are not equal to one another. If you will compare nan values for equality, the result will always be False. You can observe this in the following example.

Output:

On the contrary, two nan values are always considered unequal to one another. You can observe this in the following example.

Output:

How to remove nan from list in python?

As seen above, we cannot compare nan values to remove them from list. So, we will have to find some alternative ways. 

To check for nan values, we can convert all the elements in the list to string and then compare the string “nan” with the string values of the elements in the list. After that, we can remove the nan values from the list.

Alternatively, there are various in-built methods defined in the math module, numpy module, and the pandas module to check for nan values. We will discuss all these approaches to check for nan value and remove them from list in the next sections. 

Remove nan from list using the str() function

The most naive way to remove nan values from list is by using the str() function. The str() function is used to convert an object with any data type to a string. The str() function takes an object as an input argument and returns its string representation.

To remove nan values from list, we will first convert the value nan to a string and create an empty list newList to store the output. After that, we will traverse each element in the input list using a for loop, convert them to a string, and check if it is equal to the string “nan”. If the string representation of the element is not equal to “nan”, we will append the element to the newList using the append() method. The element whose string representation equals “nan” will be a nan value and will not be added to the newList

You can observe the entire process in the following example.

Output:

Instead of a for loop, we can also use list comprehension with the str()  function to remove the nan values from list in python as follows.

Output:

Remove nan from list using math.isnan() function

The math module in python provides us with the isnan() function with which we can check if a value is a nan value or not. The isnan() function takes a floating-point number or a number that can be converted into a floating-point number as an input argument and returns True if the input is nan. Otherwise, it returns False. You can observe this in the following example.

Output:

However, if the input argument cannot be changed into a floating-point number, the function will raise the TypeError exception as shown below.

Output:

You should understand that we cannot use math.isnan() function to check for nan values in a list if it has string values in it because it will cause an error.

To remove nan values from list in python using the math.isnan() function, we will first create an empty list named newList. After that, we will traverse each element of the list using a for loop and check if it is a nan value or not using the math.isnan() function. If the element is not a nan value, we will append it to the newList using the append() method. Otherwise, we will reject the element. After traversing the entire input list, we will get all the values that are not nan values in the newList.

You can observe this in the following example.

Output:

Instead of a for loop, we can also use list comprehension with the math.isnan() function to remove nan values from list in python as follows.

Output:

Remove nan from list using numPy.isnan() function

Instead of the isnan() function defined in the math module, we can also use the isnan() function defined in the numpy module to check if an object has a value nan or not. The numpy.isnan() function works in the same way as the math.isnan() function.

Output:

If the input argument cannot be converted to a floating-point value, the function will raise a TypeError exception as shown below.

Output:

You can use the numpy.isnan() function with a for loop to remove nan values from list. The approach used is similar to what we used while discussing the approach to remove nan values from list using math.isnan() function as you can see in the following example.

Output:

Instead of using a for loop, you can use the list comprehension and numpy.isnan() function to remove a nan value from list in python as follows.

Output:

Remove nan from list using pandas.isnull() function

Similar to math.isnan() and numpy.isnan(), the pandas module in python also provides us with the isnull() function to check for nan values. The pandas.isnull() function takes an object as an input argument and returns True if the object has nan value. Otherwise, it returns False.

Output:

You can observe that the isnull() function can also take strings as input arguments. So, we can use the pandas.isnull() function to remove nan values from list that may contain string values.

We will use the pandas.isnull() function to remove nan values from list using the for loop. The approach is similar to what we discussed to remove nan values from list using the math.isnan() function as you can see in the following example.

Output:

Instead of using a for loop, you can use the list comprehension and pandas.isnull() function to remove a nan value from list in python as follows.

Output:

Conclusion

In this article, we have discussed what is a nan value in python. We also discussed why we cannot check for a nan value using the equality operator. Finally, we discussed four ways to remove nan values from list in python. I would suggest you use math.isnan() function for checking for a nan value while removing nan values from list. This is because both numpy.isnan() and pandas.isnull() have higher execution cost compared to math.isnan(). However, if there might be string values present in the list, then you should use the approach using the str() function or the pandas.isnull() function.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *