[英]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?如何从该数组中删除所有字符串并获取具有 dtype 浮点数的新数组?
I know I can do this using python list:我知道我可以使用 python 列表来做到这一点:
[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?但是这种方式会将
numpy.ndarray
更改为list
,有没有办法在 numpy 中做到这一点?
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]]
I am not seeing a way in pure numpy
but if you are fine using pandas
to return a numpy
array:我没有看到纯
numpy
的方法,但如果您可以使用pandas
返回numpy
数组:
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
You can use np.fromiter():您可以使用 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:要进一步删除 nan 值:
r = r[~np.isnan(r)]
#[61.]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.