How To Concatenate Arrays in NumPy?

NumPy Concatenate Arrays

Often you may have two or more NumPY arrays and want to concatenate/join/merge them into a single array. Python offers multiple options to join/concatenate NumPy arrays.

Common operations include given two 2d-arrays, how can we concatenate them row wise or column wise. NumPy’s concatenate function allows you to concatenate two arrays either by rows or by columns. Let us see a couple of examples of NumPy’s concatenate function.

Let us first import the NumPy package.

# import numpy
import numpy as np

Let us create a NumPy array using arange function in NumPy. The 1d-array starts at 0 and ends at 8

array = np.arange(9)
array

We can use NumPy’s reshape function to convert the 1d-array to 2d-array of dimension 3×3, 3 rows and 3 columns. NumPy’s reshape function takes a tuple as input.

array2D_1 = array.reshape((3,3))
array2D_1

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

Let us create second 2d-array by using arange and reshape functions. The second 2d-array starts at 10 and ends at 18

>array2D_2 = np.arange(10,19).reshape(3,3)
>array2D_2

array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

NumPy concatenate

NumPy’s concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more arrays of the same shape and by default it concatenates row-wise i.e. axis=0. The resulting array after row-wise concatenation is of the shape 6 x 3, i.e. 6 rows and 3 columns.

How To Concatenate 2 NumPy Arrays Row-wise?

# concatenate 2 numpy arrays: row-wise
>np.concatenate((array2D_1, array2D_2))

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

How To Concatenate 2 NumPy Arrays Column-wise?

We can also concatenate 2 NumPy arrays by column-wise by specifying axis=1. Now the resulting array is a wide matrix with more columns than rows; in this example, 3 rows and 6 columns.

# concatenate 2 numpy arrays: column-wise
>np.concatenate((array2D_1,array2D_2),axis=1)
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

How To Concatenate more than 2 NumPy Arrays Row-wise?

NumPy’s concatenate function can be used with more than 2 arrays. Here is an example of concatenating 3 NumPy arrays row-wise. We specify the three arrays that we want to concatenate as a tuple.

# concatenate 3 numpy arrays: row-wise
>np.concatenate((array2D_1, array2D_2, array2D_1))
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],
       [ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8]])

In addition to the concatenate function, NumPy also offers two convenient functions hstack and vstack to stack/combine arrays horizontally or vertically.

Both hstack and vstack, under the hood calls on concatenate with axis =1 and axis=0 options.

Here are the examples of using hstack and vstack.

NumPy vstack example

NumPy’s vstack stacks arrays in sequence vertically i.e. row wise. And the result is the same as using concatenate with axis=0.

>np.vstack((array2D_1, array2D_2))

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

Another common use of Numpy’s hstack is to use it to combine two 1d-numpy arrays to one 2d-numpy array. For example, if we have two one-dimensional arrays,

x = np.ones(4)
y = np.arange(1,5)
print(x)
print(y)
[1. 1. 1. 1.]
[1 2 3 4]

We can use Numpy’s vstack to create 2d-array of size 2×4

print(np.vstack((x, y)))
[[1. 1. 1. 1.]
 [1. 2. 3. 4.]]

Similarly, with transpose we get 2d-array of 4×2 using vstack.

print(np.vstack((x, y)).T)
[[1. 1.]
 [1. 2.]
 [1. 3.]
 [1. 4.]]

NumPy hstack example

NumPy’s hstack stacks arrays horizontally i.e. column wise. And the result is the same as using concatenate with axis=1.

>np.hstack((array2D_1, array2D_2))

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

How to Concatenate Multiple 1d-Arrays?

NumPy’s concatenate function can also be used to concatenate more than two numpy arrays. Here is an example, where we have three 1d-numpy arrays and we concatenate the three arrays in to a single 1d-array.

Let use create three 1d-arrays in NumPy.

x = np.arange(1,3)
y = np.arange(3,5)
z= np.arange(5,7)

And we can use np.concatenate with the three numpy arrays in a list as argument to combine into a single 1d-array

>np.concatenate([x,y,z])

array([1, 2, 3, 4, 5, 6])