📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials NumPy Array Stacking and Splitting

Array Stacking and Splitting

4 min read
Concatenate, vstack, hstack, and split arrays along any axis for combining datasets.

Stacking and Splitting

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

# Stack
np.concatenate([a, b])       # [1 2 3 4 5 6]
np.vstack([a, b])             # [[1 2 3],[4 5 6]]
np.hstack([a, b])             # [1 2 3 4 5 6]
np.column_stack([a, b])       # [[1 4],[2 5],[3 6]]

# 2D stacking
A = np.ones((2,3))
B = np.zeros((2,3))
np.concatenate([A, B], axis=0)  # vertical
np.concatenate([A, B], axis=1)  # horizontal

# Split
x = np.arange(9)
np.split(x, 3)               # [0-2, 3-5, 6-8]
np.split(x, [2, 5])          # [0-1, 2-4, 5-8]
np.vsplit(A, 2)
np.hsplit(A, 3)