Skip to content

Different Ways to Concatenate NumPy Arrays in Python

Different Ways to Concatenate NumPy Arrays in Python Cover Image

In this tutorial, you’ll learn how to concatenate NumPy arrays in Python. Knowing how to work with NumPy arrays is an important skill as you progress in data science in Python. Because NumPy arrays can be 1-dimensional or 2-dimensional, it’s important to understand the many different ways in which to join NumPy arrays.

By the end of this tutorial, you’ll have learned:

  • How to use the concatenate(), vstack(), and hstack() functions
  • How to join or concatenate 1-dimensional and 2-dimensional arrays
  • How to join NumPy arrays row-wise and column-wise

How to Join 1-dimensional NumPy Arrays Column-Wise

Let’s take a look at a simple example where we want to concatenate a one-dimensional NumPy array with another one-dimensional array. When joining 1-dimensional arrays, the elements are concatenated directly.

Let’s take a look at how this works:

# Concatenating 1-dimensional NumPy Arrays
import numpy as np

array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])

joined = np.concatenate((array1, array2))

print(joined)

# Returns: [1 2 3 4 5 6 7 8]

We can see that by passing in the two arrays that they were joined in the order they were listed. If we had switched the order in the concatenate() function, the arrays would have returned [5 6 7 8 1 2 3 4].

How to Join 1-dimensional NumPy Arrays Row-Wise

In order to join 1-dimensional arrays in NumPy row-wise, we need to use the vstack() function. The concatenate() function cannot work along the row-axis. However, the vstack() function allows us to do this and returns a two-dimensional array.

Let’s take a look at an example:

# Concatenating 1-dimensional NumPy Arrays
import numpy as np

array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])

joined = np.vstack((array1, array2))

print(joined)

# Returns: 
# [[1 2 3 4]
#  [5 6 7 8]]

In the next section, you’ll learn how to concatenate two-dimensional arrays.

How to Concatenate 2-dimensional NumPy Arrays Row-Wise

To join 2-dimensional NumPy arrays row-wise, we can also use the concatenate() function. By row-wise, we mean that each array is added as though it were a row. By doing this, we concatenate along 0th axis.

The concatenate function assumes that the arrays are the same dimension, example for along the dimension along which it’s being concatenated.

Let’s take a look at an example:

# Concatenating 2-dimensional NumPy Arrays Row-Wise
import numpy as np

array1 = np.array([[1,2,3], [4,5,6]])
array2 = np.array([[11,22,33], [44,55,66]])

joined = np.concatenate((array1, array2))

print(joined)

# Returns: 
# [[ 1  2  3]
#  [ 4  5  6]
#  [11 22 33]
#  [44 55 66]]

In the next section, you’ll learn how to take on this task but join the arrays across the columns.

How to Concatenate 2-dimensional NumPy Arrays Column-Wise

In order to join NumPy arrays column-wise, we can also use the concatenate() function. In this case, however, we would use the axis=1 parameter, in order to specify that we want to join the arrays along the column axis.

Let’s take a look at an example:

# Concatenating 2-dimensional NumPy Arrays Column-Wise
import numpy as np

array1 = np.array([[1,2,3], [4,5,6]])
array2 = np.array([[11,22,33], [44,55,66]])

joined = np.concatenate((array1, array2), axis=1)

print(joined)

# Returns: 
# [[ 1  2  3 11 22 33]
#  [ 4  5  6 44 55 66]]

In the next section, you’ll learn a helper function to stack items row-wise.

How to Stack 2-dimensional NumPy Arrays Row-Wise

NumPy also provides a helper function to join arrays row-wise. This function, vstack(), calls the concatenate() function and applies the default axis=0 argument. So, why would you use this function over concatenate? Readability.

The vstack() function makes it clear that the function wants to stack the items vertically, meaning row-wise.

Let’s take a look at an example:

# Stacking NumPy Arrays Row-Wise
import numpy as np

array1 = np.array([[1,2,3], [4,5,6]])
array2 = np.array([[11,22,33], [44,55,66]])

joined = np.vstack((array1, array2))

print(joined)

# Returns: 
# [[ 1  2  3]
#  [ 4  5  6]
#  [11 22 33]
#  [44 55 66]]

In the next section, you’ll learn a helper function to stack items column-wise.

How to Stack 2-dimensional NumPy Arrays Column-Wise

Similarly, NumPy provides a helper function to stack arrays column-wise. This function, hstack(), also calls the concatenate() function under the hood and applies the axis=1 argument. Similarly, this function allows your code to be more readable, making it immediately clear that the arrays should be joined column-wise.

Let’s take a look at an example:

# Stacking NumPy Arrays Column-Wise
import numpy as np

array1 = np.array([[1,2,3], [4,5,6]])
array2 = np.array([[11,22,33], [44,55,66]])

joined = np.hstack((array1, array2))

print(joined)

# Returns: 
# [[ 1  2  3 11 22 33]
#  [ 4  5  6 44 55 66]]

Conclusion

In this tutorial, you learned how to join or concatenate NumPy arrays. You learned how to do this first for one-dimensional arrays, which can only be joined “column-wise”. Then, you learned how to use the concatenate() function to join arrays along different axes. Finally, you learned about the helper functions, vstack() and hstack(), which call the concatenate() function but help make your code more readable.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *