Home > Blockchain >  Stack 2d array on specific row
Stack 2d array on specific row

Time:11-05

I have two numpy 2d arrays, and I would like to stack them but into specific row on where I'm going to put them.

a = ([[4, 2],
      [7, 3],
      [1, 8]])

b = ([[10, 6],   (put in 3rd row)
      [9, 5]]) (put in 5th row)

Expected output = ([[4, 2],
                    [7, 3],
                    [10, 6],
                    [1, 8],
                    [9, 5]])

What is the fastest way to do this in python?

CodePudding user response:

For your particular example:

import numpy as np
a = np.array([[1, 2],[3, 4],[7, 8]])
b = np.array([[5, 6],[9, 10]])
np.insert(a,[2,3],b,axis=0)

output:

array([[ 1,  2],
   [ 3,  4],
   [ 5,  6],
   [ 7,  8],
   [ 9, 10]])
  • Related