Home > other >  new numpy array creation from two other numpy array
new numpy array creation from two other numpy array

Time:01-02

I have a 2 numpy arrays in the following format

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

and

array([[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16]])

I want to combine these to the following format

array([[[2, 2, 7, 1],[1,2,3,4]],
       [[5, 0, 3, 1],[5,6,7,8]],
       [[2, 9, 8, 8],[9,10,11,12]],
       [[5, 7, 7, 6],[13,14,15,16]]])

The real data which I am handling contains very large amount of data(5000 in one row). SO working with pandas doesnt solve the case. Is there any efficient method when the data is very huge for creating a format like this

CodePudding user response:

You are looking for stack

arr1 = np.array([[2, 2, 7, 1],
                 [5, 0, 3, 1],
                 [2, 9, 8, 8],
                 [5, 7, 7, 6]])

arr2 = np.array([[1, 2, 3, 4],
                 [5, 6, 7, 8],
                 [9, 10, 11, 12],
                 [13, 14, 15, 16]])

arr3 = np.stack((arr1, arr2), axis=1)
print(arr3)

Output

[[[ 2  2  7  1]
  [ 1  2  3  4]]

 [[ 5  0  3  1]
  [ 5  6  7  8]]

 [[ 2  9  8  8]
  [ 9 10 11 12]]

 [[ 5  7  7  6]
  [13 14 15 16]]]
  • Related