• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Remove NaN from List in Python

How to Remove NaN from List in Python

February 12, 2022 Leave a Comment

To remove NaN from a list using Python, the easiest way is to use the isnan() function from the Python math module and list comprehension.

from math import nan, isnan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = [x for x in list_with_nan if isnan(x) == False]

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

You can also use the Python filter() function.

from math import nan, isnan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = list(filter(lambda x: isnan(x) == False, list_with_nan))

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

The Python numpy module also provides an isnan() function that we can use to check if a value is NaN.

import numpy as np
from numpy import nan

list_with_nan = [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = [x for x in list_with_nan if np.isnan(x) == False]

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

When working with lists, it can be valuable to be able to easily filter and remove unwanted values from your list.

One such situation where you may want to remove values from a list is if you have a lot of NaN in your list.

We can easily remove all NaN from a list using Python with list comprehension. List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

We can use list comprehension to remove all instances of a value in a list easily.

To check if an item in a list is NaN, we can use the isnan() function from the math module.

Below is the code which will allow you to remove all NaN from a list using list comprehension in Python.

from math import nan, isnan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = [x for x in list_with_nan if isnan(x) == False]

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

Removing NaN from List with Python filter() Function

The Python filter() function is a built-in function that allows you to process an iterable and extract items that satisfy a given condition.

We can use the Python filter() function to extract all the items in a list of numbers which are not NaN.

Below is some example code showing you how to remove NaN from a list using the filter() function.

from math import nan, isnan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = list(filter(lambda x: isnan(x) == False, list_with_nan))

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

Using the Python numpy Module to Remove NaN from List

We can also remove NaN values using the Python numpy module. The numpy module provides an isnan() function that we can use to check if a value is NaN.

We can use the numpy isnan() function in combination with list comprehension to remove NaN values from a list.

The example below is almost identical to the first example except for we are now using the numpy module.

import numpy as np
from numpy import nan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = [x for x in list_with_nan if np.isnan(x) == False]

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

Removing NaN from a pandas DataFrame or Series in Python

If you are working with the pandas module in Python, you can use the Python pandas dropna() function to remove NaN values from a pandas DataFrame or column.

To drop the rows or columns with NaN values, you can use the dropna() function in the following ways.

df = df.dropna() #drops rows with missing values

df["Column 1"] = df["Column 1"].dropna() #drops rows with missing values in column "Column 1"

df = df.dropna(axis=1) #drop columns with missing values

Removing Any Value from List Using Python

In a very similar way, we can remove any value from a list using list comprehension.

For example, if we wanted to remove all empty strings from a list in Python, we could do that easily with list comprehension in Python by adjusting the code above.

list_of_strings = [“This”,””,”is”,”a”,”list”,””,”with”,”empty”,””,”strings”,”.”]
list_without_empty_strings = [x for x in list_of_strings if x != “”]

print(list_without_empty_strings)

#Output:
[‘This’, ‘is’, ‘a’, ‘list’, ‘with’, ’empty’, ‘strings’, ‘.’]

Another example is if wanted to remove all zeros from a list, we could do that easily with list comprehension in Python by adjusting the code above.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = [x for x in list_of_numbers if x != 0]

print(list_without_zeros)

#Output:
[1,4,2,-4,3,-1]

Hopefully this article has been useful for you to learn how to remove NaN from a list in Python.

Other Articles You'll Also Like:

  • 1.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 2.  Python Decrement Counter with -= Decrement Operator
  • 3.  Remove None From List Using Python
  • 4.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 5.  Check if Variable is Tuple in Python
  • 6.  Count Number of Keys in Dictionary in Python
  • 7.  Check if Variable is Float in Python
  • 8.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 9.  Convert Set to List in Python
  • 10.  Using Python to Find Maximum Value in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy