Reputation: 191
I am new to Numpy and I am not an expert programmer at all...
This is my issue:
I have array a
and array b
(b < a).
I want to substitute some rows of a
with all the rows of b
(in order).
The rows in a
to be substituted have one value in common with b
rows.
for example:
a
is:
1 a 2
3 b 4
0 z 0
5 c 6
0 y 0
b
is
1 z 1
1 y 1
In this case I will want to substitute rows 3 and 5 in a
with 1 and 2 in b
. The arrays are very big and, as in the example, there are some character types (so I set the array to dtype=object
).
Upvotes: 1
Views: 131
Reputation: 46530
First, I would recommend changing your dtype
from object
to [('a',int),('letter','S1'),('b',int)]
. What this does is allow you to have columns of different dtypes (note the length of that dtype list is the number of columns). Then you have:
In [63]: a = np.array(
....: [(1,'a',2),(3,'b',4),(0,'z',0),(5,'c',6),(0,'y',0)],
....: dtype=[('a',int),('letter','S1'),('b',int)])
In [64]: a
Out[64]:
array([(1, 'a', 2), (3, 'b', 4), (0, 'z', 0), (5, 'c', 6), (0, 'y', 0)],
dtype=[('a', '<i8'), ('letter', 'S1'), ('b', '<i8')])
In [65]: a['a']
Out[65]: array([1, 3, 0, 5, 0])
In [66]: a['b']
Out[66]: array([2, 4, 0, 6, 0])
In [67]: a['letter']
Out[67]:
array(['a', 'b', 'z', 'c', 'y'],
dtype='|S1')
Then you can use the same dtype for b
:
In [69]: b = np.array([(1,'z',1),(1,'y',1)], dtype=a.dtype)
In [70]: b
Out[70]:
array([(1, 'z', 1), (1, 'y', 1)],
dtype=[('a', '<i8'), ('letter', 'S1'), ('b', '<i8')])
In [71]: b['letter']
Out[71]:
array(['z', 'y'],
dtype='|S1')
Now, you can simply replace the parts of a
with the parts of b
where the letters match:
In [73]: a
Out[73]:
array([(1, 'a', 2), (3, 'b', 4), (0, 'z', 0), (5, 'c', 6), (0, 'y', 0)],
dtype=[('a', '<i8'), ('letter', 'S1'), ('b', '<i8')])
In [74]: b
Out[74]:
array([(1, 'z', 1), (1, 'y', 1)],
dtype=[('a', '<i8'), ('letter', 'S1'), ('b', '<i8')])
In [75]: for row in b:
....: a[a['letter']==row['letter']] = row
....:
In [76]: a
Out[76]:
array([(1, 'a', 2), (3, 'b', 4), (1, 'z', 1), (5, 'c', 6), (1, 'y', 1)],
dtype=[('a', '<i8'), ('letter', 'S1'), ('b', '<i8')])
Upvotes: 1