Overview: 5 Ways to Combine Arrays

NumPy provides five useful functions to combine arrays:

1. numpy.stack()
2. numpy.vstack()
3. numpy.hstack()
4. numpy.dstack()
5. numpy.column_stack()

The main difference: How the arrays are combined and the dimensions of the resulting array.

1. Prepare Two NumPy Arrays

First, let's understand the basic concept. We will use the exact same two arrays to explore and compare all five functions:

import numpy as np a = np.array([10, 20, 30]) b = np.array([40, 50, 60]) print(a) print(b)

Output:

[10 20 30] [40 50 60]

Both arrays are one-dimensional:

print(a.shape) # (3,) print(b.shape) # (3,)
Here, (3,) means each array has three elements and one dimension.

2. Using numpy.stack()

Purpose: Joins arrays along a new axis.

Syntax

np.stack((a, b), axis=0)

Step 1: Stack along axis 0

result = np.stack((a, b), axis=0) print(result) print(result.shape)

Output:

[[10 20 30] [40 50 60]] (2, 3)
Visual Understanding (axis=0)
Array a: [10, 20, 30] Array b: [40, 50, 60] Create a new axis (axis=0) -> stacked as rows: [[10, 20, 30], [40, 50, 60]]

The new array has two rows and three columns with shape (2, 3).

Step 2: Stack along axis 1

result = np.stack((a, b), axis=1) print(result) print(result.shape)

Output:

[[10 40] [20 50] [30 60]] (3, 2)

Notice that the values are paired by index position:

  • 10 with 40 → Row 0: [10, 40]
  • 20 with 50 → Row 1: [20, 50]
  • 30 with 60 → Row 2: [30, 60]

Step 3: Important Rule

The input arrays must have the exact same shape when using stack().
x = np.array([1, 2, 3]) y = np.array([4, 5]) # np.stack((x, y)) # Raises ValueError: all input arrays must have the same shape
Common Use Case: Use stack() when you want to create a new dimension, such as combining multiple images, recordings, or experimental runs into a new batch dimension.

3. Using numpy.vstack()

Purpose: Stacks arrays vertically, placing one row group below another (row-wise).

Syntax

np.vstack((a, b))

Step 1: Execute with 1D Arrays

result = np.vstack((a, b)) print(result) print(result.shape)

Output:

[[10 20 30] [40 50 60]] (2, 3)

For these 1D arrays, vstack() first treats each array as a row and then stacks the rows on top of each other.

Step 2: Understand with 2D Arrays

x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) result = np.vstack((x, y)) print(result) print(result.shape)

Output:

[[1 2] [3 4] [5 6] [7 8]] (4, 2)
Vertical Joining Diagram
Array x: 1 2 3 4 Array y: 5 6 7 8 Joined Vertically (x on top of y): 1 2 3 4 5 6 7 8

Rule: The number of columns must match for 2D arrays. Here, both inputs have 2 columns.

Remember: vstack() is useful when you want to add rows (e.g., adding new samples, records, or observations) to a dataset.

4. Using numpy.hstack()

Purpose: Joins arrays horizontally, placing elements or columns side by side.

Syntax

np.hstack((a, b))

Step 1: Using 1D Arrays

a = np.array([10, 20, 30]) b = np.array([40, 50, 60]) result = np.hstack((a, b)) print(result) print(result.shape)

Output:

[10 20 30 40 50 60] (6,)
Important: With 1D arrays, hstack() joins the elements into one longer 1D array (simple concatenation).

Step 2: Using 2D Arrays

x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) result = np.hstack((x, y)) print(result) print(result.shape)

Output:

[[1 2 5 6] [3 4 7 8]] (2, 4)
Horizontal Joining Diagram
Array x: Array y: 1 2 5 6 3 4 7 8 Joined Horizontally (side by side): 1 2 5 6 3 4 7 8

Rule: For 2D arrays, the number of rows must match. Here, both arrays have 2 rows.

Remember: hstack() is useful when adding columns side by side to an existing 2D dataset.

5. Using numpy.dstack()

Purpose: Joins arrays along the third axis, also called the depth axis.

Syntax

np.dstack((a, b))

Step 1: Use 1D Arrays

a = np.array([10, 20, 30]) b = np.array([40, 50, 60]) result = np.dstack((a, b)) print(result) print(result.shape)

Output:

[[[10 40] [20 50] [30 60]]] (1, 3, 2)
Why is the shape (1, 3, 2)?
  • 1 — one row
  • 3 — three positions along the column dimension
  • 2 — two values at each position, one from each input array (the depth)

Unlike hstack(), dstack() creates a three-dimensional result for these 1D inputs.

Step 2: Use 2D Arrays

x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) result = np.dstack((x, y)) print(result) print(result.shape)

Output:

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

Here, the result has shape (2, 2, 2). Think of each position in the original matrices as having a depth of two:

  • At position (0, 0): values are 1 and 5 → [1, 5]
  • At position (0, 1): values are 2 and 6 → [2, 6]
  • At position (1, 0): values are 3 and 7 → [3, 7]
  • At position (1, 1): values are 4 and 8 → [4, 8]
Remember: dstack() is useful for combining data along a depth dimension, such as combining separate color planes into RGB image channels or stacking 3D volumetric measurements.

6. Using numpy.column_stack()

Purpose: Converts 1D arrays into columns and combines them side by side into a 2D array.

Syntax

np.column_stack((a, b))

Step 1: Use 1D Arrays

a = np.array([10, 20, 30]) b = np.array([40, 50, 60]) result = np.column_stack((a, b)) print(result) print(result.shape)

Output:

[[10 40] [20 50] [30 60]] (3, 2)
Visual Breakdown
Array a (as col 1): Array b (as col 2): 10 40 20 50 30 60 Result (2D array with 2 columns): 10 40 20 50 30 60

Each original 1D array is treated as a vertical column.

Step 2: Compare with hstack() (Crucial Difference!)

print(np.hstack((a, b))) print(np.column_stack((a, b)))

Output:

[10 20 30 40 50 60] [[10 40] [20 50] [30 60]]
Most Important Distinction:
  • hstack() concatenates 1D arrays end-to-end into one 1D array.
  • column_stack() turns 1D arrays into columns of a 2D array.

Step 3: What about 2D Arrays?

x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) print(np.column_stack((x, y)))

Output:

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

For 2D inputs, column_stack() behaves exactly like hstack().

Remember: column_stack() is ideal when creating a table or dataframe from separate 1D feature arrays, such as Student IDs, Marks, or Ages.

7. Comparison Table: All Five Functions

Here is a complete side-by-side comparison of the five array-combining functions:

Feature stack() vstack() hstack() dstack() column_stack()
Main purpose Creates a new axis Vertical joining Horizontal joining Depth joining Combines columns
Joining direction Depends on axis Rows (vertical) Columns / elements (horizontal) Third axis (depth) Columns (horizontal)
For 1D arrays Adds a dimension Creates rows (2D) Concatenates elements (1D) Creates 3D array Creates columns (2D)
Output shape for a & b (2, 3) with axis 0
(3, 2) with axis 1
(2, 3) (6,) (1, 3, 2) (3, 2)
New axis? Yes Not always Not always Uses third axis Converts 1D to 2D cols
Common use Batching data / time frames Adding rows / samples Adding columns to 2D data Image channels / depth grids Building tabular data from 1D lists

8. Quick Visual Revision

Using a = [10, 20, 30] and b = [40, 50, 60]:

1. stack(axis=0)
[[10 20 30] [40 50 60]]

New axis added; shape becomes (2, 3).

2. vstack()
[[10 20 30] [40 50 60]]

Stacked vertically; one array placed below the other.

3. hstack()
[10 20 30 40 50 60]

For 1D arrays, produces one longer 1D array of shape (6,).

4. dstack()
[[[10 40] [20 50] [30 60]]]

Combines values along the depth axis; shape is (1, 3, 2).

5. column_stack()
[[10 40] [20 50] [30 60]]

Each 1D array becomes a column; shape is (3, 2).


Stacking Functions Quiz

Test your understanding of array stacking functions. Click any option to see instant feedback.

1. What is the output shape when two 1D arrays of shape (4,) are combined using np.stack((a, b), axis=0)?
A) (8,)
B) (2, 4)
C) (4, 2)
D) (1, 4, 2)
2. What is the main difference between np.hstack((a, b)) and np.column_stack((a, b)) for 1D arrays?
A) There is no difference; both produce 1D arrays
B) hstack() produces a 1D array, while column_stack() turns 1D arrays into columns of a 2D array
C) column_stack() produces a 3D array
D) hstack() raises a ValueError for 1D arrays
3. Which function would you choose to combine red, green, and blue 2D image matrices into an RGB image?
A) np.vstack()
B) np.hstack()
C) np.dstack()
D) np.column_stack()
4. If you attempt to stack two arrays with shapes (3,) and (4,) using np.stack(), what occurs?
A) A ValueError is raised because stack() requires identical input shapes
B) NumPy automatically pads the shorter array with zeros
C) It returns an array of shape (2, 4)
D) It truncates the longer array
5. For 2D arrays, which of the following is true regarding column_stack()?
A) It raises a ValueError
B) It stacks them along the depth axis
C) It behaves identically to hstack()
D) It stacks them vertically as rows