Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join two 2D numpy array with one row over another

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading