join two array in Python
<p>I am new to Python. I use np.lib.recfunctions.join_by to join two array, but the results is wrong. Here is the example and results:</p>

<pre><code>a = np.array([('a',1),('b',2),('b',2),('c',3)],dtype=[('key','&lt;U1'),('x','&lt;i4')])
b = np.array([('a',-1),('b',-2)],dtype=[('key','&lt;U1'),('y','&lt;i4')])

np.lib.recfunctions.join_by('key', a, b, jointype='outer').data

array([(u'a', 1, -1), (u'b', 2, -2), (u'b', 2, 0), (u'c', 3, 0)], 
      dtype=[])
</code></pre>

<p>why the second joined b shows 0 not -2</p>

<p>what I want is </p>

<pre><code>(a,1,-1),(b,2,-2),(b,2,-2),(c,3,0)
</code></pre>

<p>How to do it?</p>

<p>Thanks</p>

<p>Had some time to examine a bit more in the options.  I think part of the information is being hidden using  '.data' to get your return.  Consider the following </p>

<pre><code>from numpy.lib import recfunctions as rfn

a  # array a and dtype
array([('a', 1), ('b', 2), ('b', 2), ('c', 3)], 
      dtype=[('key', '&lt;U5'), ('x', '&lt;i4')])

a_u  # np.unique(a) and dtype
array([('a', 1), ('b', 2), ('c', 3)], 
      dtype=[('key', '&lt;U5'), ('x', '&lt;i4')])

b  # array b and dtype
array([('a', -1), ('b', -2)], 
      dtype=[('key', '&lt;U5'), ('x', '&lt;i4')])

# join examples

rfn.join_by('key', a, b, jointype='outer')  # join b to a
masked_array(data = [('a', 1, -1) ('b', 2, -2) ('b', 2, --) ('c', 3, --)],
             mask = [(False, False, False) (False, False, False) (False, False, True) (False, False, True)],
       fill_value = ('N/A', 999999, 999999),
            dtype = [('key', '&lt;U5'), ('x1', '&lt;i4'), ('x2', '&lt;i4')])

rfn.join_by('key', a_u, b, jointype='outer')  # join b to a_u
masked_array(data = [('a', 1, -1) ('b', 2, -2) ('c', 3, --)],
             mask = [(False, False, False) (False, False, False) (False, False, True)],
       fill_value = ('N/A', 999999, 999999),
            dtype = [('key', '&lt;U5'), ('x1', '&lt;i4'), ('x2', '&lt;i4')])

rfn.join_by('key', a, b, jointype='inner').data  # join b to a, with data
array([('a', 1, -1), ('b', 2, -2), ('b', 2, 0)], 
      dtype=[('key', '&lt;U5'), ('x1', '&lt;i4'), ('x2', '&lt;i4')])

rfn.join_by('key', a_u, b, jointype='inner').data  # join b to a_u, with data
array([('a', 1, -1), ('b', 2, -2)], 
      dtype=[('key', '&lt;U5'), ('x1', '&lt;i4'), ('x2', '&lt;i4')])
</code></pre>

<p>If you skip .data, more information (useful?) is returned.
In your simple case, just returning the unique records in array 'a' takes care of any issues you seem to have with interpretation.</p>

<p>I think you will have to investigate in more detail what you need returned given you have a limited number of fields participating in the join.</p>

python arrays numpy