PaleNeutron
PaleNeutron

Reputation: 3215

How to remove all string elements from a numpy object array

origin array is like:

array([nan, nan, 'hello', ..., nan, 'N', 61.0], dtype=object)

How can I remove all string from this array and get a new array with dtype float?

I know I can do this using python list:

[i for i in x if type(i) == float]

but this way will change numpy.ndarray to list, is there a way to do this in numpy?

Upvotes: 0

Views: 858

Answers (3)

pakpe
pakpe

Reputation: 5479

You can use np.fromiter():

a = np.array([np.nan, np.nan, 'hello', ..., np.nan, 'N', 61.0], dtype=object)
r = np.fromiter((x for x in a if type(x) == float), dtype=float)

print(r)
#[nan nan nan 61.]

To further remove nan values:

r = r[~np.isnan(r)]
#[61.]

Upvotes: 0

bluefcat
bluefcat

Reputation: 21

You can try something like below.

import numpy as np
a = array([np.nan, np.nan, 'hello', ..., np.nan, 'N', 61.0], dtype=object)
a = a[[isinstance(i, float) for i in a]]

Upvotes: 1

cpage
cpage

Reputation: 39

I am not seeing a way in pure numpy but if you are fine using pandas to return a numpy array:

import panadas as pd
import numpy as np

arr = np.array([np.nan, np.nan, 'hello', np.nan, 'N', 61.0], dtype=object)
pd.to_numeric(pd.Series(arr), errors='coerce').dropna().values

Upvotes: 0

Related Questions