How to modify one column of a selected row from a

2019-07-28 06:31发布

问题:

I am looking for an easy way to modify one field of a numpy structured array of a selected line of it. Here is my SWE :

import numpy as np
dt=np.dtype([('name',np.unicode,80),('x',np.float),('y',np.float)])
a=np.array( [('a',0.,0.),('b',0.,0.),('c',0.,0.) ],dtype=dt)
b=a.copy()
a[a['name']=='a']['x']=1
print a==b # return [ True  True  True]

In this example, the a==b results should return [False True True].Actually, I would like to selected the line of my array from the 'name' field and modify the value of one field of it (here 'x').

回答1:

I found the answer... The point is the position of the field and of the mask. You need to apply the mask to the field column, not looking for the field of the masked array :

a['x'][a['name']=='a']=1
print a==b # returns [False  True  True]