The problem is that both a and b are 1D arrays, and therefore only one axis is attached to them.
Instead, you can use vstack (v for vertical):
>>> np.vstack((a,b)) array([[1, 2, 3, 4], [5, 6, 7, 8]])
Additionally, row_stack is an alias of the vstack function:
>>> np.row_stack((a,b)) array([[1, 2, 3, 4], [5, 6, 7, 8]])
It is also worth noting that several arrays of the same length can be stacked at once. For example, np.vstack((a,b,x,y)) will have four lines.
Under the hood, vstack works by making sure that each array has at least two dimensions (using atleast_2D ) and then calls concatenate to combine these arrays on the first axis ( axis=0 ).
source share