How to modify one column of a selected row from a

2019-07-28 06:03发布

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条回答
对你真心纯属浪费
2楼-- · 2019-07-28 06:53

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]
查看更多
登录 后发表回答