I am trying to assign values from one recarray (in_arr) into another (out_arr) based on an identifier string in one of the columns. To assign the values correctly, the strings in id must match.
Some constraints:
- the number of elements in
in_arrcan be smaller or larger than the number inout_arr - every identifier in
in_arris represented inout_arr, not necessarily the other way round - if the number in
in_arris larger, entries will repeat and any - single one - of these can be assigned - every identifier in
out_arris unique - the element order of the result does not matter
- I'd rather not loop throug every element ;-)
Here is some code:
my_dtype = [('id', 'S3'), ('val', int)]
in_arr = np.array([('xyz', 1), ('abc', 2), ('abc', 2)], dtype=my_dtype)
out_arr = np.array([('abc', 0), ('asd', 0), ('qwe', 0), ('xyz', 0), ('def', 0)], dtype=my_dtype)
msk_in, msk_out = ... # some magic
out_arr[msk_out]['val'] = in_arr[msk_in]['val'] # <-- other ways to assign also work for me...
out_arr
array([(b'abc', 2), (b'asd', 0), (b'qwe', 0), (b'xyz', 1), (b'def', 0)],
dtype=[('id', 'S3'), ('val', '<i8')])
The closest, I came for replacing my "magic part" is by borrowing from this question. But this only gives me the correct indices, not the correct order.
np.where(np.isin(out_arr['id'], in_arr['id']))[0]
array([0, 3])
structuredones like this. Keepingidandvaltogether can be handy, but it may complicate your think. Also be ware of this indexing:out_arr[msk_out]['val'] = ```. I would put [val']` first, since themsk_outindexing is boolean and makes a copy.out_val=out_arr['val']etc; that should be aviewthat can be referenced and assigned. But I'm suggesting that just because it might make the code easier to read. I haven't looked at your logic enough to suggest anything more.idvalues, e.g.,xyz-->'1', so I kept the secondvalin my answer.