Overview: 5 Ways to Combine Arrays
NumPy provides five useful functions to combine arrays:
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:
Output:
Both arrays are one-dimensional:
(3,) means each array has three elements and one dimension.
2. Using numpy.stack()
Purpose: Joins arrays along a new axis.
Syntax
Step 1: Stack along axis 0
Output:
Visual Understanding (axis=0)
The new array has two rows and three columns with shape (2, 3).
Step 2: Stack along axis 1
Output:
Notice that the values are paired by index position:
10with40→ Row 0:[10, 40]20with50→ Row 1:[20, 50]30with60→ Row 2:[30, 60]
Step 3: Important Rule
stack().
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
Step 1: Execute with 1D Arrays
Output:
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
Output:
Vertical Joining Diagram
Rule: The number of columns must match for 2D arrays. Here, both inputs have 2 columns.
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
Step 1: Using 1D Arrays
Output:
hstack() joins the elements into one longer 1D array (simple concatenation).
Step 2: Using 2D Arrays
Output:
Horizontal Joining Diagram
Rule: For 2D arrays, the number of rows must match. Here, both arrays have 2 rows.
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
Step 1: Use 1D Arrays
Output:
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
Output:
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
1and5→[1, 5] - At position (0, 1): values are
2and6→[2, 6] - At position (1, 0): values are
3and7→[3, 7] - At position (1, 1): values are
4and8→[4, 8]
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
Step 1: Use 1D Arrays
Output:
Visual Breakdown
Each original 1D array is treated as a vertical column.
Step 2: Compare with hstack() (Crucial Difference!)
Output:
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?
Output:
For 2D inputs, column_stack() behaves exactly like hstack().
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]:
New axis added; shape becomes (2, 3).
Stacked vertically; one array placed below the other.
For 1D arrays, produces one longer 1D array of shape (6,).
Combines values along the depth axis; shape is (1, 3, 2).
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)?
2. What is the main difference between np.hstack((a, b)) and np.column_stack((a, b)) for 1D arrays?
3. Which function would you choose to combine red, green, and blue 2D image matrices into an RGB image?
4. If you attempt to stack two arrays with shapes (3,) and (4,) using np.stack(), what occurs?
5. For 2D arrays, which of the following is true regarding column_stack()?
hstack()