Numpy: Replacing values ​​in recarray

I am new to numpy and I am trying to replace the value in recarray. So I have this array:

import numpy as np
d = [('1', ''),('4', '5'),('7', '8')]
a = np.array(d, dtype=[('first', 'a5'), ('second', 'a5')])

I would like to do something like this:

ind = a=='' #Replace all blanks
a[ind] = '12345'

but it does not work properly. I was able to do this:

col = a['second']
ind = col=='' #Replace all blanks
col[ind] = '54321'
a['second'] = col

Which works, but I would rather do this throughout the entire re-parsing. Does anyone have a better solution?

+3
source share
2 answers

The numit operations (individually), which you can perform any function in all elements of the array at once without a loop, do not work with recursions, as far as I know. You can only do this with individual columns.

, , , , :

for fieldname in a.dtype.names:
    ind = a[fieldname] == ''
    a[fieldname][ind] = '54321'

, , , , ndarray. , ( ), .

+3

:

a[np.where(a['second']=='')[0][0]]['second']='12345'
-1

All Articles