1

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.

2 Answers 2

2

IIUC, you could stack on the first axis, and reshape:

np.stack((a,b), axis=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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Use the axis parameter to skip the swap: np.stack((a,b),axis=1).reshape(-1,2)
1

You can column_stack + reshape:

out = np.column_stack((a,b)).reshape(4,2)

Output:

array([[1, 2],
       [5, 6],
       [3, 4],
       [7, 8]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.