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)
答案 0 :(得分:1)
以下是使用np.core.defchararray.replace
和np.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]])