Python Numpy Structured Array (recarray) assigning values into slices Python Numpy Structured Array (recarray) assigning values into slices numpy numpy

Python Numpy Structured Array (recarray) assigning values into slices


When you say test['ifAction'] you get a view of the data.When you say test[['ifAction','ifDocu']] you are using fancy-indexing and thus get a copy of the data. The copy doesn't help you since modifying the copy leaves the original data unchanged.

So a way around this is to assign values to test['ifAction'] and test['ifDocu'] individually:

test['ifAction'][0]=1test['ifDocu'][0]=1

For example:

import numpy as nptest=np.rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),   (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],   dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')])print(test[['ifAction','ifDocu']])# [(0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)]test['ifAction'][0]=1test['ifDocu'][0]=1print(test[['ifAction','ifDocu']][0])# (1, 1)test['ifAction'][0:10]=1test['ifDocu'][0:10]=1print(test[['ifAction','ifDocu']])# [(1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1)]

For a deeper look under the hood, see this post by Robert Kern .