Search code examples
pythonarraysnumpymergeconcatenation

Python/Numpy - merge 2 items in each "row" of 1d array


I have a quick question:

I have an array like this:

array([('A', 'B'),
       ('C', 'D'),
      dtype=[('group1', '<U4'), ('group2', '<U4')])

And I would like to combine group1 and group2 into 1 like this:

array([('A_B'),
       ('C_D'),
      dtype=[('group3', '<U4')])

I tried some different things from other answers like this:

array_test = np.array([])
for group in array_test:
    combi = np.append(combi,np.array(group[0]+"_"+group[1]))

this does give me a new array with what I want, but when I try to add it to the array I get an error which I can't figure out (don't really know what it means):

np.append(test_array, combi, axis=1)

numpy.AxisError: axis 1 is out of bounds for array of dimension 1

I tried other thing with concaternate as well but it gave the same error

could someone help me?


Solution

  • The error means that you try to append a 1D array (shape(n,)) to another 1D array along the the second dimension (axis=1) which is impossible as your arrays have only one dimension.

    If you don't specify the axis (or axis=0) you'll end up, however, with just a 1D array like array(['A_B', 'C_D']). To get a structured array as requested you need to create a new array like np.array(combi, dtype=[('group3', '<U4')]).

    You can do the same vectorized without a loop:

    np.array(np.char.add(np.char.add(a['group1'], '_'), a['group2']), dtype=[('group3', '<U4')])