NumPy: How to Join Arrays (column-wise & row-wise)

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a powerful library for numerical computing in Python. It provides efficient and versatile capabilities for handling arrays and matrices, which are essential in scientific computing, data analysis, and machine learning. A common operation when working with NumPy arrays is joining them together, either column-wise or row-wise. This tutorial will walk you through the steps of joining arrays using NumPy with practical code examples.

Prerequisites:

  • A basic understanding of Python.
  • NumPy library installed (pip install numpy).

Creating NumPy Arrays

Before we jump into joining arrays, let’s create some arrays that we will be using throughout this tutorial:

import numpy as np

# Creating two arrays of shape (2, 3)
A = np.array([[1, 2, 3],
              [4, 5, 6]])
B = np.array([[7, 8, 9],
              [10, 11, 12]])

print("Array A:", A)
print("Array B:", B)

Joining Arrays Row-wise

One way to join arrays is by appending them row-wise, which means adding the rows of one array to another. In NumPy, you can do this using the np.vstack() (vertical stack) function.

# Stacking arrays A and B row-wise
C = np.vstack((A, B))

print("Array C after row-wise stacking:", C)

Output:

Array C after row-wise stacking: [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

You can also use the np.concatenate() function and specify the axis along which to concatenate. Here’s how to do it row-wise:

# Concatenating arrays A and B row-wise using np.concatenate()
D = np.concatenate((A, B), axis=0)

print("Array D after row-wise concatenation:", D)

Output:

Array D after row-wise concatenation: [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Joining Arrays Column-wise

Column-wise stacking refers to adding the columns of one array to another. To perform column-wise stacking in NumPy, you’ll use the np.hstack() (horizontal stack) function.

# Stacking arrays A and B column-wise
E = np.hstack((A, B))

print("Array E after column-wise stacking:", E)

Output:

Array E after column-wise stacking: [[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

Similarly to np.concatenate() for row-wise joining, you can use it for column-wise stacking by changing the axis parameter to 1:

# Concatenating arrays A and B column-wise using np.concatenate()
F = np.concatenate((A, B), axis=1)

print("Array F after column-wise concatenation:", F)

Output:

Array F after column-wise concatenation: [[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

Joining Arrays with Different Dimensions

In some cases, you want to join arrays that have different dimensions. NumPy’s np.vstack() and np.hstack() can still handle this as long as the dimensions are compatible. For example, you can stack a one-dimensional array with a two-dimensional array if they have the same length along the axis of stacking.

# One-dimensional array
row_vec = np.array([13, 14, 15])

# Stacking a one-dimensional array with a two-dimensional array row-wise
G = np.vstack((A, row_vec))

print("Array G after stacking with one-dimensional array row-wise:", G)

Output:

Array G after stacking with one-dimensional array row-wise: [[ 1  2  3]
 [ 4  5  6]
 [13 14 15]]

When working with column-wise stacking and one-dimensional arrays, you may need to reshape the array to make it two-dimensional before stacking it. Here’s how to do it:

# One-dimensional array
col_vec = np.array([13, 14]).reshape(-1, 1)

# Stacking a one-dimensional array with a two-dimensional array column-wise
H = np.hstack((A, col_vec))

print("Array H after stacking with one-dimensional array column-wise:", H)

Output:

Array H after stacking with one-dimensional array column-wise: [[ 1  2  3 13]
 [ 4  5  6 14]]

Advanced Joining with np.column_stack() and np.row_stack()

NumPy provides the np.column_stack() and np.row_stack() functions, which are equivalent to np.hstack() and np.vstack() for common use cases. However, np.column_stack() has the added feature of automatically converting one-dimensional arrays into columns before stacking:

# Using np.column_stack() to stack one-dimensional arrays as columns
one_d_array1 = np.array([13, 14])
one_d_array2 = np.array([15, 16])

I = np.column_stack((one_d_array1, one_d_array2))

print("Array I after column stacking one-dimensional arrays:", I)

Output:

Array I after column stacking one-dimensional arrays: [[13 15]
 [14 16]]

For row stacking, np.row_stack() provides a simple way to stack arrays row-wise:

# Using np.row_stack() to stack one-dimensional arrays as rows

J = np.row_stack((one_d_array1, one_d_array2))

print("Array J after row stacking one-dimensional arrays:", J)

Output:

Array J after row stacking one-dimensional arrays: [[13 14]
 [15 16]]

Conclusion

Joining arrays is a key operation in NumPy that’s important for constructing larger datasets from smaller ones or simply reorganizing data. The vstack, hstack, and concatenate functions are versatile tools in your NumPy arsenal. By understanding how to use these functions to join arrays row-wise and column-wise, you can manage and manipulate data more effectively for your analytical or machine learning tasks.