Remove NaN From List in Python

  1. Remove NaN From the List in Python Using the math.isnan() Method
  2. Remove NaN From the List in Python Using the numpy.isnan() Method
  3. Remove NaN From the List of Strings in Python
  4. Remove NaN From the List in Python Using the pandas.isnull() Method

This tutorial will look into various methods to find and remove the Nan values from the list in Python. The NaN value in programming means Not a Number, which means the variable’s value is not a number.

If a NaN value occurs in an array or a list, it can create problems and errors in the calculations. We will also look into ways to remove the string values nan from the list in this tutorial. We can remove the NaN or 'nan' values from the list, by using the following methods.

Remove NaN From the List in Python Using the math.isnan() Method

The math.isnan(value) method takes a number value as input and returns True if the value is a NaN value and returns False otherwise. Therefore we can check if there a NaN value in a list or array of numbers using the math.isnan() method.

We need the math.isnan() method because if float('NaN') == float('NaN') returns False in Python or we can say that two NaN values are not equal in Python. The below example code demonstrates how to use the math.isnan() method to remove the NaN value from the list.

import math

mylist = [1,2,float('nan'),8,6,4,float('nan')]
print(mylist)
newlist = [x for x in mylist if math.isnan(x) == False]
print(newlist)

Output:

[1, 2, nan, 8, 6, 4, nan]
[1, 2, 8, 6, 4]

Remove NaN From the List in Python Using the numpy.isnan() Method

The np.isnan(array) method, takes the array as input and returns True for the corresponding index if it is NaN value and returns False otherwise.

The below example code demonstrates how to remove the NaN values from the list using the numpy.isnan() method:

import numpy as np

mylist = [1,2,float('nan'),8,6,4,float('nan')]
print(mylist)
newlist = [x for x in mylist if np.isnan(x) == False]
print(newlist)

Output:

[1, 2, nan, 8, 6, 4, nan]
[1, 2, 8, 6, 4]

Remove NaN From the List of Strings in Python

Now, let’s suppose that the number list is converted to string type, and we want to check if it contains any NaN values. After converting into the string type, the NaN value becomes a string equal to 'nan' and can be easily detected and remove by comparing it with 'nan'.

The below example code demonstrates how we can remove the NaN value from the list of string data type:

mylist = [1,2,'nan',8,6,4,'nan']
mylist = [str(x) for x in mylist]
print(mylist)
newlist = [x for x in mylist if x != 'nan']
print(newlist)

Output:

['1', '2', 'nan', '8', '6', '4', 'nan']
['1', '2', '8', '6', '4']

Remove NaN From the List in Python Using the pandas.isnull() Method

The pandas.isnull(obj) takes a scalar or an array-like obj as input and returns True if the value is equal to NaN, None, or NaT; otherwise, it returns False.

The example code demonstrates how to use the pandas.isnull() method to remove the NaN values from Python’s list.

import pandas as pd

mylist = [1,2,float('nan'),8,float('nan'),4,float('nan')]
print(mylist)
newlist = [x for x in mylist if pd.isnull(x) == False]
print(newlist)

Output:

[1, 2, nan, 8, nan, 4, nan]
[1, 2, 8, 4]

Now suppose we do not know the type of the list or if the list contains the data of various data types. In this case, we can check and remove the NaN values and 'nan' values from the list using the pandas.isnull() method by comparing each value of the list with the 'nan' value.

We can use the pandas.isnull() method because, unlike the previously mentioned methods, the pandas.isnull() method does not return an error if the string data type is given as input. Therefore we can use the pandas.isnull() method to remove the NaN and 'nan' value from the list or an array in Python.

The below example code demonstrates how to use the pandas.isnull() method and the 'nan' value to remove NaN and 'nan' values from the list in Python.

import pandas as pd

mylist = ['John',23,'nan','New York',float('nan')]
print(mylist)
newlist = [x for x in mylist if pd.isnull(x) == False and x != 'nan']
print(newlist)

Output:

['John', 23, 'nan', 'New York', nan]
['John', 23, 'New York']
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python List

  • Convert List to Set in Python
  • Flatten a List in Python