How to modify one column of a selected row from a numpy structured array

假装没事ソ 提交于 2019-12-24 17:19:35

问题


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]


来源:https://stackoverflow.com/questions/36693399/how-to-modify-one-column-of-a-selected-row-from-a-numpy-structured-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!