I want to delete nan
from a numpy array. Lets say my numpy array contains:
np_array = ["123","pqr","123",nan,"avb", nan]
Expected output:
["123","pqr","123","avb"]
If we do it in pandas using pandas.dropna()
it deletes the whole row which I don't want to do. I just want to delete the value and reduce the array size.
Is there any possible way to do so?
You can't use np.isnan
because the NaN
s are strings in your array but you can use boolean indexing by comparing with the string: "nan"
:
>>> import numpy as np
>>> np_array = np.array(["123","pqr","123",np.nan,"avb", np.nan])
>>> np_array[np_array != 'nan']
array(['1234', 'pqr', '123', 'avb'],
dtype='<U4')
isnan()
should do the trick. Working minimal example on how to do it:
>>> import numpy as np
>>> np_array = np.array([1,2,3,np.nan,4])
>>> np_array
array([ 1., 2., 3., nan, 4.])
>>> np_array = np_array[~np.isnan(np_array)]
>>> np_array
array([ 1., 2., 3., 4.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With