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!


import_contacts

You may also like:

Related Posts

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

  • Initialize list with zeros in Python
    25 October

    Four Ways To Initialize List With Zeros In Python

    Table of ContentsWhy Initialize List with Zeros in Python?How Can We Initialize List With Zeros In Python?Initialize List With Zeros Using * Operator In PythonInitialize List With Zeros Using  itertools.repeat() FunctionInitialize List With Zeros Using  Generator Comprehension In PythonInitialize List With Zeros Using  List Comprehension In PythonConclusion Lists in Python are one of the most […]

  • 06 October

    How to find index of all occurrences in list in Python

    Table of ContentsThe easiest method – Using list count() methodThe Basic Method – Using for loopsThe shortest method – Using the enumerate() functionThe fastest method – Using a numpy arrayUsing itertools moduleUsing more-itertools LibraryUsing index() function List is a data type in Python. It is used to store multiple values in one variable. If you […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Sort list alphabetically in Python
    31 May

    Sort list alphabetically in Python

    Table of ContentsUse of the sort() function to sort list alphabetically in Python.Use of the sorted() function sort list alphabetically in Python.Use of the QuickSort algorithm sort list alphabetically in Python. 💡 Outline You can use sort() method of list to sort list alphabetically in Python [crayon-619868d40cef5093373832/] In this tutorial, we will discuss different ways […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.