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
🌐
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) ...
Discussions

python - Removing "nan" values from a numpy array - Stack Overflow
I am aware that a solution would ... the "nan" values and then convert it back to a numpy array, but I would like to know if there is a more direct approach, exclusively within the numpy package (and other than making a huge "for" loop and iterating over everything). Thank you. ... Does that mean you seek to delete those rows? If yes, there is a numpy.delete function ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to remove nan values from numpy.ndarray - Stack Overflow
I have some numpy.ndarray variables. They include nan values and I want to drop each nan value from them. Arrays contain int, float, str, etc. values. An example to these arrays: ['A' 'B' 'C' 'D' ... More on stackoverflow.com
🌐 stackoverflow.com
filter out values from 3d numpy array - apply boolean index?

I want remove the second dimension if index 1 in the third dimension is 0

What do you mean by remove? In numpy, your arrays have to have a uniform shape. You can't have "gaps" where things are missing.

You can replace the value with NaN or something. Is that what you want to do?

Or are you saying that if you see a zero anywhere in the third axis, you want to return a 2d array with the second axis removed? (So sometimes you'll have a 2d array and sometimes you'll have a 3d one, depending on if there's a 0 in the third axis?)

What shape do you want the result to have?

More on reddit.com
🌐 r/learnpython
6
5
May 26, 2019
find mean of 2 numpy arrays without using the 0 values
I'd set 0 values to nan and then use np.nanmean More on reddit.com
🌐 r/learnpython
18
3
April 9, 2023
🌐
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. ...
🌐
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:
🌐
CodeGym
codegym.cc › java blog › learning python › remove nan using python
Remove NaN Using Python
August 14, 2024 - We convert the numpy array to a pandas Series. Use dropna() to remove the NaN values.
🌐
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 ...
🌐
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 - These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np.isnan() function.
Find elsewhere
🌐
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 - NumPy, the foundational library for numerical computing, uses NaN as a floating-point sentinel value defined by the IEEE 754 standard. Knowing how to detect, remove, and work around NaN values is essential for producing correct analytical results. Before removing NaN values you need to find them. The function np.isnan() returns a boolean array of the same shape as the input, with True wherever a NaN appears.
🌐
Delft Stack
delftstack.com › home › howto › numpy › numpy remove nan values
How to Remove Nan Values From a NumPy Array | Delft Stack
February 2, 2024 - These two ways involve isnan() function from math library and isnull function from pandas library. Both these functions check whether an element is nan or not and return a boolean result.
🌐
py4u
py4u.org › blog › how-to-remove-nan-values-from-a-given-numpy-array
How to Remove NaN Values from a Given NumPy Array?
Key Property: np.nan != np.nan (so you cannot check for NaN with ==—use np.isnan() instead). ... Create a mask of non-NaN values using ~np.isnan(arr) (the ~ operator negates the boolean mask), then index the array with this mask.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to remove nan values from numpy arrays
5 Best Ways to Remove NaN Values from NumPy Arrays - Be on the Right Side of Change
February 20, 2024 - It then uses np.isnan to create a boolean mask where True corresponds to NaN values. The tilde (~) operator is used to invert this mask, and the resultant boolean array is used to index and filter out the NaN values. The numpy.compress function can be combined with numpy.isnan to remove NaN values from an array.
🌐
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 mathprint (math.isnan (56))print (math.isnan (-45))print (math.isnan (+45.34))print (math.isnan (math.inf))print (math.isnan (float(“nan”)))print (math.isnan (float(“inf”)))print (math.isnan (float(“-inf”)))print (math.isnan (math.nan)) ... import mathimport 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])b1 = [not math.isnan(number) for number in x1]b2 = [not math.isnan(number) for number in x2]b3 = [not math.isnan(number) for number in x3]print(b1)print(b2)print(b3)
🌐
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 ...
🌐
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
🌐
IncludeHelp
includehelp.com › python › how-to-remove-nan-values-from-a-given-numpy-array.aspx
How to remove NaN values from a given NumPy array?
Suppose we need to create a NumPy array of length n, and each element of this array would be e a single value (say 5). To remove NaN values from a given NumPy array, you can use a method provided by NumPy called the full() method.
🌐
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.
🌐
IncludeHelp
includehelp.com › python › how-can-i-remove-nan-from-list-numpy-array.aspx
Python - How can I remove Nan from list NumPy array?
October 8, 2023 - # Import numpy import numpy as np # Creating a numpy array arr = np.array([2,np.nan,4,3,4,np.nan,7,4,7,np.nan,4,3]) # Display original array print("Original Array:\n",arr,"\n") # Determining whether arr is numeric or not res = [ x for x in arr if str(x) != 'nan'] # Display result print("Result is:\n",res,"\n")
🌐
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]]]