Concatenate arrays into matrix and reshape as required

Dear all,
I am trying to obtain a matrix in this form:

[
[‘a1’, ‘b1’, ‘c1’],
[‘a2’, ‘b2’, ‘c2’],
[‘a3’, ‘b3’, ‘c3’],
[‘a4’, ‘b4’, ‘c4’]
]

from the arrays:

A = np.array([‘a1’, ‘a2’, ‘a3’, ‘a4’])
B = np.array([‘b1’, ‘b2’, ‘b3’, ‘b4’])
C = np.array([‘c1’, ‘c2’, ‘c3’, ‘c4’])

I have tried different combinations but none useful:

np.concatenate((A,B,C),axis=1)
Traceback (most recent call last):

File “”, line 4, in
np.concatenate((A,B,C),axis=1)

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

np.concatenate((A,B,C),axis=2)
Traceback (most recent call last):

File “”, line 1, in
np.concatenate((A,B,C),axis=2)

AxisError: axis 2 is out of bounds for array of dimension 1

np.concatenate((A,B,C))
Out[44]:
array([‘a1’, ‘a2’, ‘a3’, ‘a4’, ‘b1’, ‘b2’, ‘b3’, ‘b4’, ‘c1’, ‘c2’, ‘c3’,
‘c4’], dtype=’<U2’)

np.vstack((A, B, C))
Out[45]:
array([[‘a1’, ‘a2’, ‘a3’, ‘a4’],
[‘b1’, ‘b2’, ‘b3’, ‘b4’],
[‘c1’, ‘c2’, ‘c3’, ‘c4’]], dtype=’<U2’)

np.concatenate((A,B,C)).reshape(4,3)
Out[46]:
array([[‘a1’, ‘a2’, ‘a3’],
[‘a4’, ‘b1’, ‘b2’],
[‘b3’, ‘b4’, ‘c1’],
[‘c2’, ‘c3’, ‘c4’]], dtype=’<U2’)

What would be the correct combination?
Thank you

1 Like

No worries, I finally found it:

np.column_stack((A,B,C))
Out[48]:
array([[‘a1’, ‘b1’, ‘c1’],
[‘a2’, ‘b2’, ‘c2’],
[‘a3’, ‘b3’, ‘c3’],
[‘a4’, ‘b4’, ‘c4’]], dtype=’<U2’)

2 Likes