np.array with inhomogeneos data (str+float)

时间:2015-10-06 08:41:00

标签: python numpy

i'am importing data from excel. The matrix is a mix of floats (or strings that are convertable to floats) and strings and I need to do operations with the floats, ignoring the strings. How can i set the strings to 0?

Example: i have an array a: a = np.array(['1','.5','day'],['month','.5','7.3'])

I want to convert that array to: a = ([1,.5,0],[0,.5,7.3])

edit: it worked out for me with the following code:

shape = np.shape(a)
for i in range(shape[0]):
    for j in range(shape[1]):
        if np.core.defchararray.isdigit((np.core.defchararray.replace(a[i,j],'.','')))==False:
            a[i,j]=0

a=np.array(a,float)

1 个答案:

答案 0 :(得分:1)

以下是使用np.core.defchararray.replacenp.core.defchararray.isdigit -

组合的一种方法
a[~np.core.defchararray.isdigit(np.core.defchararray.replace(a,'.',''))]=0
out = a.astype(float)

示例运行 -

In [2]: a
Out[2]: 
array([['1', '.5', 'day'],
       ['month', '.5', '7.3']], 
      dtype='|S5')

In [3]: a[~np.core.defchararray.isdigit(np.core.defchararray.replace(a,'.',''))] = 0

In [4]: a.astype(float)
Out[4]: 
array([[ 1. ,  0.5,  0. ],
       [ 0. ,  0.5,  7.3]])