Joining Arrays in NumPy

NumPy makes it easy to combine multiple arrays into one using a variety of methods. These operations are commonly referred to as joining or concatenating arrays.

Whether you’re working with 1D sequences, 2D matrices, or higher-dimensional data, NumPy provides flexible functions to merge arrays along different axes.

  • Concatenation: Merge arrays end-to-end using np.concatenate().
  • Stacking: Combine arrays vertically or horizontally using np.vstack(), np.hstack(), or np.stack().

These tools are essential for tasks such as dataset preparation, reshaping data, or building complex structures for machine learning models and data pipelines.



Joining Arrays Using np.concatenate()

The np.concatenate() function is one of the most commonly used ways to join two or more arrays along an existing axis. The arrays must have the same shape, except in the dimension corresponding to the axis along which you are joining.

Here's a simple example of joining two 1D arrays:

python
import numpy as np

# Create two 1D arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Join them using concatenate
result = np.concatenate((a, b))

print(result)

How It Works:

  • np.concatenate((a, b)): Joins the arrays a and b end-to-end along axis 0 (default for 1D arrays).
  • Both arrays must be the same shape (number of elements) except for the concatenation axis.

Output

[1 2 3 4 5 6]

You can also use axis explicitly when working with higher-dimensional arrays. For example:

python
# Create two 2D arrays (2 rows, 3 columns)
import numpy as np
a2 = np.array([
    [1, 2, 3],
    [4, 5, 6]
    ])
b2 = np.array([
    [7, 8, 9], 
    [10, 11, 12]
    ])

# Concatenate along axis 0 (rows)
result0 = np.concatenate((a2, b2), axis=0)

# Concatenate along axis 1 (columns)
result1 = np.concatenate((a2, b2), axis=1)

print("Axis 0:", result0)
print("Axis 1:", result1)
  • axis=0: Concatenates along the first axis (rows in 2D arrays — vertical stacking).
  • axis=1: Concatenates along the second axis (columns in 2D arrays — horizontal stacking).
  • All arrays must match in shape along all other axes.

Output

Axis 0:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
Axis 1:
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

💡 Tip: Use np.concatenate() when you want to merge arrays without introducing a new axis.
It joins along an existing axis only — it does not allow creating a new axis like np.stack() does.


Stacking Arrays Using np.stack()

Unlike concatenate(), which joins arrays along an existing axis, np.stack() joins arrays along a new axis. This is useful when you want to group arrays together and increase their dimensionality.

The arrays you want to stack must have the exact same shape. You can specify the axis along which the new dimension will be inserted.

python
import numpy as np

# Create two 1D arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Stack along a new axis (axis=0)
stacked0 = np.stack((a, b), axis=0)

# Stack along a new axis (axis=1)
stacked1 = np.stack((a, b), axis=1)

print("Axis 0:")
print(stacked0)
print("Axis 1:")
print(stacked1)

How It Works:

  • np.stack((a, b), axis=0): Adds a new dimension at axis 0 → results in a 2×3 array.
  • np.stack((a, b), axis=1): Adds a new dimension at axis 1 → results in a 3×2 array.
  • The shape of the output depends on the specified axis.

Output

Axis 0:
[[1 2 3]
 [4 5 6]]
Axis 1:
[[1 4]
 [2 5]
 [3 6]]

You can also use np.stack() with higher-dimensional arrays, as long as all input arrays have the same shape.

python
# Two 2D arrays
import numpy as np      
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])

# Stack along a new axis
stacked0 = np.stack((x, y), axis=0)
stacked1 = np.stack((x, y), axis=1)

print("Stacked0 shape:", stacked0.shape)
print(stacked0)

print("Stacked1 shape:", stacked1.shape)
print(stacked1)

How It Works:

  • stacked0 = np.stack((x, y), axis=0): This stacks x and y along a new axis at position 0.
    The original arrays x and y each have shape (2, 2).
    After stacking: shape becomes (2, 2, 2).
    Think of it like combining them into a batch of 2 arrays — one on top of the other.
  • stacked1 = np.stack((x, y), axis=1): This stacks x and y along a new axis at position 1.
    Again, original shape is (2, 2).
    After stacking: shape becomes (2, 2, 2) — but the structure is different.
    Here, the arrays are paired column-wise across the new axis.

Output

Stacked0 shape: (2, 2, 2)
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
Stacked1 shape: (2, 2, 2)
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

💡 Tip: Use np.stack() when you want to preserve the structure of your arrays but add a new dimension for advanced indexing or batch processing.


Stacking Arrays Vertically and Horizontally

NumPy provides convenient functions like np.vstack() and np.hstack() to combine arrays vertically (row-wise) or horizontally (column-wise). These are shorthand alternatives to np.concatenate() with fixed axes.

Vertical Stack – np.vstack()

np.vstack() stacks arrays on top of each other, increasing the number of rows. It’s equivalent to np.concatenate(..., axis=0) for 2D arrays.

python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

v_stacked = np.vstack((a, b))

print(v_stacked)

Output

[[1 2 3]
 [4 5 6]]

Horizontal Stack – np.hstack()

np.hstack() joins arrays side-by-side, increasing the number of columns. It’s equivalent to np.concatenate(..., axis=1) for 2D arrays.

python
# Same 1D arrays from before
h_stacked = np.hstack((a, b))

print(h_stacked)

Output

[1 2 3 4 5 6]

For 2D arrays, hstack() joins them along the columns (axis=1), and vstack() joins along the rows (axis=0):

python
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])

v_result = np.vstack((x, y))
h_result = np.hstack((x, y))

print("Vertical stack:")
print(v_result)
print("Horizontal stack:")
print(h_result)

Output

Vertical stack:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Horizontal stack:
[[1 2 5 6]
 [3 4 7 8]]
  • vstack(): Adds rows → increases height.
  • hstack(): Adds columns → increases width.
  • All arrays must have compatible shapes.

💡 Tip: Use vstack() and hstack() when working with consistent shapes and need quick row-wise or column-wise merging.


Frequently Asked Questions

How do I join arrays in NumPy?

You can join arrays using functions like np.concatenate, np.vstack, np.hstack, and np.stack depending on the axis and shape.


What is the difference between np.concatenate and np.stack?

np.concatenate joins arrays along an existing axis, while np.stack adds a new axis and joins arrays along that.


Can I join arrays of different shapes in NumPy?

No, arrays must have compatible shapes based on the joining method. For example, np.concatenate requires all arrays to match in all dimensions except the one being joined.


What does np.vstack do?

np.vstack stacks arrays vertically (row-wise), joining them along the first axis (axis=0).


Is joining arrays memory efficient?

Joining arrays creates a new array and copies the data, so it might not be memory efficient for very large arrays. Use it carefully in performance-sensitive contexts.



What's Next?

Next, we’ll dive into Splitting Arrays in NumPy — a technique that allows you to divide one array into multiple sub-arrays.