Two numpy arrays, lets say
a = np.array([[1,2], [3,4]])
b = np.array([[5,6], [7,8]])
I would like to combine two arrays into one single array such that the results looks like below array
np.array([[1,2],
[5,6],
[3,4],
[7,8]])
I tried using concatenate, merge function, but cannot able to find pythonic way to solve this. Is there is any in built function to solve my problem.
CodePudding user response:
IIUC, you could stack
, swap the axes, and reshape
:
np.stack([a,b]).swapaxes(0,1).reshape((-1,2))
Or use np.c_
and reshape
:
np.c_[a,b].reshape((-1,2))
Output:
array([[1, 2],
[5, 6],
[3, 4],
[7, 8]])
CodePudding user response:
You can column_stack
reshape
:
out = np.column_stack((a,b)).reshape(4,2)
Output:
array([[1, 2],
[5, 6],
[3, 4],
[7, 8]])