Python Numpy return ndarray 中的字符串元素

Python Numpy return elements in ndarray that are strings

我正在尝试获取 ndarray 中的字符串元素。即排除整数和浮点数的元素。

假设我有这个数组:

x = np.array([1,'hello',2,'world'])

我想要 return:

array(['hello','world'],dtype = object)

我试过 np.where(x == np.str_) 来获取满足该条件的索引,但它不起作用。

非常感谢任何帮助。

当你运行x = np.array([1,'hello',2,'world'])时,numpy会把所有东西都转换成字符串类型。

如果是一维数组,可以使用:

y = np.array([i for i in x if not i.replace(".","",1).replace("e+","").replace("e-","").replace("-","").isnumeric()])

获取所有非数字值。

它可以识别所有带负号和和e+/e-的浮点数)

喜欢,输入:x = np.array([1,'hello',+2e-50,'world', 2e+50,-2, 3/4, 6.5 , "!"]) 输出将是:array(['hello', 'world', '!'], dtype='<U5')

你可以创建一个函数来完成它,并遍历数组:

def getridofnumbers(num):
    try:
        x = int(num)
    except:
        return True
    return False

output = np.array([i for i in x if getridofnumbers(i)])

如果我们想保留所有 numpy 优点(广播等),我们可以使用矢量化(或 np.frompyfunc)将其转换为 ufunc

import numpy as np
#vectorize the fucntion, with a boolean return type
getrid = np.vectorize(getridofnumbers, otypes=[bool])

x[getrid(x)]
array(['hello', 'world'], dtype='<U11')

#or ufunc, which will require casting:
getrid = np.frompyfunc(getridofnumbers, 1, 1)
x[getrid(x).astype(bool)]