To remove NaN values from a NumPy array x:

x = x[~numpy.isnan(x)]
Explanation

The inner function numpy.isnan returns a boolean/logical array which has the value True everywhere that x is not-a-number. Since we want the opposite, we use the logical-not operator ~ to get an array with Trues everywhere that x is a valid number.

Lastly, we use this logical array to index into the original array x, in order to retrieve just the non-NaN values.

Answer from jmetz on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Remove NaN (np.nan) from an array | note.nkmk.me
January 23, 2024 - In NumPy, to remove rows or columns containing NaN (np.nan) from an array (ndarray), use np.isnan() to identify NaN and methods like any() or all() to extract rows or columns that do not contain NaN. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-rows-in-numpy-array-that-contains-non-numeric-values
How to Remove Rows in Numpy Array that Contains Non-Numeric Values? - GeeksforGeeks
December 13, 2025 - This method filters rows by checking if they contain any NaN. Rows without NaN are kept; rows with even one NaN are removed. ... import numpy as np a = np.array([[10.5, 22.5, 3.8], [41, np.nan, np.nan]]) clean = a[~np.isnan(a).any(axis=1)] print(clean)
🌐
Skytowner
skytowner.com › explore › removing_rows_containing_nan_in_a_numpy_array
Removing rows containing NaN in a NumPy array
To remove rows containing NaN in a NumPy array, we can use a combination of the isnan(~) and any(~) methods.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-91.php
NumPy: Remove all rows in a NumPy array that contain non-numeric values - w3resource
August 29, 2025 - Write a NumPy program to remove all rows in a NumPy array that contain non-numeric values. ... # Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing various data types including integers, NaN (Not a Number), and booleans x = np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [True, False, True]]) # Printing a message indicating the original array will be displayed print("Original array:") # Printing the original array 'x' with its elements print(x) # Printing a message indicating the removal of all non-numeric elements from the array prin
🌐
Statology
statology.org › home › how to remove nan values from numpy array (3 methods)
How to Remove NaN Values from NumPy Array (3 Methods)
May 27, 2022 - The following code shows how to remove NaN values from a NumPy array by using the isnan() function:
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_removing_missing_data.htm
NumPy - Removing Missing Data
In this example, we use np.isnan() function combined with any() function to create a mask that identifies rows containing NaN values. We then use this mask to filter out and remove those rows from the original 2D array − · import numpy as ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-nan-values-from-a-given-numpy-array
How to remove NaN values from a given NumPy array? - GeeksforGeeks
January 14, 2026 - import numpy as np arr = np.array([[12, 5, np.nan, 7], [2, 61, 1, np.nan], [np.nan, 1, np.nan, 5]]) res = arr[~np.isnan(arr)] print("2D array converted to 1D after removing NaNs ->", res)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-remove-nan-values-from-a-given-numpy-array
How to remove NaN values from a given NumPy array? - GeeksforGeeks
September 7, 2022 - Many times NumPy arrays may contain NaN values that need to be removed to ensure the array is free from unnecessary or invalid data. This can be achieved using the np.isnan() function along with the Bitwise NOT operator.
🌐
Medium
medium.com › @keycomputereducation › numhow-to-remove-nan-value-from-numpy-array-in-python-736939c5bd14
NumHow to remove nan value from Numpy array in Python? | by Key computer Education | Medium
May 1, 2022 - import numpy as npx1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])x2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])x3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])print(x1[np.isfinite(x1)])print(x2[np.isfinite(x2)])print(x3[np.isfinite(x3)])
🌐
ProjectPro
projectpro.io › recipes › drop-all-missing-values-from-numpy-array
How to remove nan values from numpy array - Projectpro
December 23, 2022 - Droping the missing values or nan values can be done by using the function "numpy.isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy.logical_not()" where the boolean values ...
🌐
Codemia
codemia.io › home › knowledge hub › how do i remove nan values from a numpy array?
How do I remove NaN values from a NumPy array? | Codemia
September 23, 2025 - The tilde operator (~) negates each boolean, so you keep only the elements that are not NaN. This creates a new array; the original is unchanged. For a 2-D array you can remove entire rows or columns that contain at least one NaN.
🌐
Python Pool
pythonpool.com › home › tutorials › remove nan from a python list: math.isnan, numpy, and pandas
Remove NaN from a Python List: math.isnan, NumPy, and pandas
July 13, 2026 - This version removes both None and float NaN values. Use this style when each skipped case needs a comment, log entry, or separate handling step. A loop is also helpful when you need to count removed items or collect rejected entries for a quality report. NumPy creates a boolean mask with np.isnan().
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-110.php
Python NumPy: Remove nan values from a given array - w3resource
August 29, 2025 - # Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing integers and 'np.nan' (representing missing values) x = np.array([200, 300, np.nan, np.nan, np.nan, 700]) # Creating a 2D NumPy array 'y' containing integers and 'np.nan' (representing missing values) y = np.array([[1, 2, 3], [np.nan, 0, np.nan], [6, 7, np.nan]]) # Printing the original array 'x' print("Original array:") print(x) # Removing 'np.nan' values from array 'x' and storing the result in 'result' result = x[np.logical_not(np.isnan(x))] # Printing the array 'x' after removin
🌐
Medium
medium.com › @ryan_forrester_ › remove-nan-from-lists-in-python-a-complete-guide-d6656077f664
Remove NaN from Lists in Python: A Complete Guide | by ryan | Medium
January 7, 2025 - Sometimes you’ll have lists inside lists, and removing NaN values gets trickier. Here’s a robust solution: import math import numpy as np def clean_nested_list(nested_data): """Remove NaN values from lists within lists""" # If we're looking at a list, process each item in it if isinstance(nested_data, list): # Process each item, but only keep non-NaN values return [ clean_nested_list(item) for item in nested_data if not (isinstance(item, float) and math.isnan(item)) and item is not None ] # If it's not a list, return it unchanged return nested_data # Test with a complex nested structure nested_data = [ [1, float('nan'), [3, None]], [math.nan, 5, [6, float('nan')]], [7, [8, None, 9]] ] cleaned = clean_nested_list(nested_data) print("Cleaned nested data:") print(cleaned) # Output: [[1, [3]], [5, [6]], [7, [8, 9]]]