remove nan from list

How to Remove NaN from List in Python

Python lists are a very useful data structures that allow you to store different kinds of data types in one place, in a compact manner. It also allows you to store NaN (Not a Number) values in list. These values are generated when you call a mathematical function such as int() or float() on a non numerical value. Such values can trip your list processing, if it is dependent on numerical values. There are various ways to remove NaN from list in python. In this article, we will learn how to remove NaN from Python List.


How to Remove NaN from List in Python

Let us say you have the list [1, 2, ‘NaN’, 4, 5]. Here are the different ways to remove NaN from Python List.


1. Using math.isnan() method

math.isnan() function checks an input value and returns true if it is NaN (Not a number) and false if it is a number. So we us a list comprehension to loop through the items of a list, and check each item with isnan() function. It will be false for all numbers and true for NaN value. In the newlist we include only those values that evaluate to False when checked with isnan() function.

import math

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

Here is the output you will get

[1,2,'nan',4,5]
[1,2,4,5]


2. Using numpy.isnan() method

numpy library too provides an isnan() function that accepts an array input and returns True if an item corresponding to given index is NaN, and False if it is not NaN.

import numpy

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

Here is the output you will get

[1,2,'nan',4,5]
[1,2,4,5]


3. Using string operators

In this case, you can simply loop through the list and check each item to see if it is ‘nan’ or not. If it is ‘nan’ exclude it, else include it.

import numpy

mylist = [1,2,float('nan'),4,5]
print(mylist)
newlist = [x for x in mylist if x!='nan']
print(newlist)

Here is the output you will get

[1,2,'nan',4,5]
[1,2,4,5]

In this article, we have learnt 3 different ways to remove NaN from list of items. You can customize these methods to remove any other kind of items from your python lists.

Also read:

Delete All Files Except One in Linux
How to Mount Remote Directory or Filesystem in Linux
How to Check Bad Sectors in Linux
How to Convert Markdown to HTML in Linux
How to Create SFTP User for Specific Directory

Leave a Reply

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