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
imhans33imhans33
asked Jan 2, 2022 at 8:48
1 Answer 1
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]]]
answered Jan 2, 2022 at 9:00
Sign up to request clarification or add additional context in comments.
2 Comments
habibalsaki
@imhans33, if that works, please accept it as the correct answer.
imhans33
acceted.. pls upvote the qs too
lang-py
np.stack
with appropriateaxis
argument, e.g.np.stack((array1, array2), axis=1)
.