In this tutorial we will see how to join different numpy arrays horizontally and vertically using different methods.We can merge multiple numpy arrays to form a new numpy array which will contain all the arrays given as input. Numpy provides the stacking concept which contains a number of functions that will help in merging the arrays.

How to join two numpy arrays vertically?

We can perform vertical stacking to join two numpy arrays vertically with the help of np.vstack() method which will add the numpy arrays as new rows of new numpy array.In this case the array grows in vertical direction.We can implement the vstack() operation by below mentioned code.

import numpy as np
A=np.zeros((3,3))
print("Array A is:")
print(A)
B=np.ones((3,3))
print("Array B is:")
print(B)
print("After performing vertical stack Array grows as:")
np.vstack((A, B))

Output:

Array A is:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
Array B is:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
After performing vertical stack Array grows as:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

In the above example we can see that  array A and B have the same shape. np.vstack() method takes a tuple of numpy arrays as an argument which must be in the same shape in all directions except vertically downwards direction across rows. If we pass a tuple of 1-D arrays as an argument, they must be of the same length.

How to join two numpy arrays horizontally?

We can join two numpy arrays horizontally by performing horizontal stacking with the help of np.hstack() method which adds numpy arrays as new columns of the output array.Hence,Array grows horizontally.

Below given implementation shows how hstack() works:

import numpy as np
A=np.zeros((3,3))
print("Array A is:")
print(A)
B=np.ones((3,3))
print("Array B is:")
print(B)
print("After performing vertical stack Array grows as:")
np.hstack((A, B))

Output:

Array A is:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Array B is:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
After performing horizontal stack Array grows as:
array([[0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.]])

Here, np.hstack() takes a tuple of numpy arrays as argument.Also, the arrays in the tuple can be of any shape along any axis except the horizontal direction across columns. If we want to pass 1-D arrays in the tuple, they must be of the same length.

Now,we will see some functions that generally work with one-dimensional and two-dimensional arrays and perform stacking between multiple arrays.These functions are column_stack() and row_stack().

How to make 2-D numpy arrays when columns are given as 1-D arrays?

We can make 2-D numpy arrays from columns given in the form of 1-D numpy arrays using the  np.column_stack() method which stacks arrays as columns to form new two-dimensional array.The operation can be understood from following example :

import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
c=np.array([7,8,9])
print("After performing column_stack() the new two-dimensional array is:")
print(np.column_stack((a,b,c)))

Output:

After performing column_stack() the new two-dimensional array is:
[[1 4 7]
 [2 5 8]
 [3 6 9]]

In the above example, np.column_stack() takes a tuple of arrays as argument and returns a numpy array  formed by stacking the given arrays. The 1-D arrays passed as input must be of the same length. We can also pass 2-D arrays along with 1-D arrays as arguments given the condition that the arrays have columns of the same length.

How to join rows  of a 2-D numpy array which are given as 1-D numpy arrays?

For joining 1-D arrays to make them rows of the new 2-D array, we can use np.row_stack() method which joins the 1-D arrays in such a way that they constitute rows of the new 2-D array. The following example shows how to perform the operation.

import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
c=np.array([7,8,9])
print("After performing row_stack() the new two-dimensional array is:")
print(np.row_stack((a,b,c)))

Output:

After performing row_stack() the new two-dimensional array is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Here, np.row_stack() method takes a tuple of numpy arrays as input and returns a new numpy array which has input arrays as it’s rows. Here we can also stack 2-D arrays along with 1-D arrays with np.row_stack() method given the condition that rows of the input arrays must be of same length.

Conclusion

In this tutorial we have seen how to join arrays horizontally and vertically using different methods of Numpy module like hstack(),vstack(), row_stack() and column_stack(). We have also seen the details of the different methods about their input and output.

References:

Happy Learning 🙂

Leave A Comment