NumPy
1 min read
Updated 4 Aug 2026
11. Combining & Splitting Arrays
a = np.array([[1, 2], [3, 4]])
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate([a, b], axis=0) # stack rows -> 3x2
np.vstack([a, b]) # same as concatenate axis=0
np.hstack([a, a]) # side by side -> 2x4
np.column_stack([[1,2,3],[4,5,6]]) # -> [[1 4],[2 5],[3 6]]
np.stack([np.array([1,2]), np.array([3,4])]) # NEW axis -> shape (2,2)
m = np.arange(6)
np.split(m, 3) # [array([0,1]), array([2,3]), array([4,5])]
np.array_split(m, 4) # uneven split allowed
| Function | Behavior |
|---|---|
concatenate |
join along an existing axis |
stack |
join along a new axis (increases ndim) |
vstack / hstack / dstack |
vertical / horizontal / depth |
column_stack |
treat 1-D arrays as columns |
split |
equal parts (errors if unequal) |
array_split |
allows unequal parts |
⚠️ Common Mistake:
concatenaterequires matching shapes on all axes except the join axis. Mismatched dimensions →ValueError.
⭐ Interview Question:
stackvsconcatenate?concatenatekeeps ndim the same;stackadds a new dimension.