Python Numpy return elements in ndarray that are strings
When you run x = np.array([1,'hello',2,'world']), numpy converts everything to string type.
If it is one dimensional array, you can use:
y = np.array([i for i in x if not i.replace(".","",1).replace("e+","").replace("e-","").replace("-","").isnumeric()])
to get all non-numeric values.
It can identify all floats with negative sign and and e+/e- )
like, for input: x = np.array([1,'hello',+2e-50,'world', 2e+50,-2, 3/4, 6.5 , "!"])
output will be : array(['hello', 'world', '!'], dtype='<U5')
I am trying to get the elements in an ndarray that are strings. That is, exclude the elements that are integers and floats.
Lets say I have this array:
x = np.array([1,'hello',2,'world'])
I want it to return:
array(['hello','world'],dtype = object)
I've tried doing np.where(x == np.str_) to get the indices where that condition is true, but it's not working.
Any help is much appreciated.