Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete 'nan' or reduce length of numpy array if array contains nan after converting to numpy from pandas [duplicate]

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?

like image 587
Rohan Nagalkar Avatar asked Oct 14 '25 04:10

Rohan Nagalkar


2 Answers

You can't use np.isnan because the NaNs 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')
like image 161
MSeifert Avatar answered Oct 20 '25 09:10

MSeifert


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.])
like image 36
Christian W. Avatar answered Oct 20 '25 07:10

Christian W.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!