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.
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.
filter(lambda v: v==v, x)
works both for lists and numpy array since v!=v only for NaN
python - Removing "nan" values from a numpy array - Stack Overflow
python - How to remove nan values from numpy.ndarray - Stack Overflow
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.comfind mean of 2 numpy arrays without using the 0 values
This is probably because the np.isnan() is failing to deal with string types among your possible element types in collection. You could try to use panda's isnull() to remove NaN values.
import pandas as pa
import numpy as np
a = ['A', np.nan, np.nan, 1.67, 8]
a = [x for x in a if not pa.isnull(x)]
print(a)
There's function in pandas called pandas.DataFrame.dropna which drops all the columns with nan values. Just create DataFrame if you didn't, and do df.dropna().