简体   繁体   中英

new numpy array creation from two other numpy array

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

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]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM