vstack, hstack & column_stack
Convenience wrappers around concatenate: vstack (rows on top of each other), hstack (side by side) and column_stack (1D arrays become columns).
Watch the lesson
1 video on CodeWithMunnaX
np.concatenate is powerful but you must think about axis numbers and reshape 1D arrays yourself. NumPy gives three friendlier helpers whose names describe the direction: vertical stack, horizontal stack, and column stack. These are the functions taught in the "concatenate, vstack, hstack" video, and the ones you will use most.
Definition
np.vstackstacks arrays vertically (row-wise, one below another); 1D arrays of length n are first treated as rows of shape (1, n).np.hstackstacks arrays horizontally (column-wise, side by side); for 1D arrays it simply joins them end to end.np.column_stackstacks 1D arrays as columns of a 2D array; 2D inputs are joined side by side likehstack.
- vstack = concatenate along axis 0, after promoting 1D inputs to 2D rows.
- hstack = concatenate along axis 1 for 2D+ inputs, but along axis 0 for 1D inputs — that is why
hstackof two 1D arrays gives a longer 1D array, not two columns. - column_stack is the one to use when you have separate 1D arrays (runs, balls, fours) and want them as columns of a table.
Syntax
python
np.vstack(tup, *, dtype=None, casting="same_kind")
np.hstack(tup, *, dtype=None, casting="same_kind")
np.column_stack(tup)| Parameter | Meaning | Default |
|---|---|---|
tup | tuple/list of arrays | required |
dtype | force the result dtype (vstack / hstack) | None |
casting | dtype conversion rule (vstack / hstack) | "same_kind" |
| Function | Equivalent to | Shape rule |
|---|---|---|
vstack | concatenate(axis=0) with 1D → (1, n) | number of columns must match |
hstack | concatenate(axis=1) (axis 0 for 1D) | number of rows must match |
column_stack | 1D → (n, 1) columns, then concatenate(axis=1) | lengths / rows must match |
dstack | stack along the 3rd axis (depth) | rows and columns must match |
Example — two players, three helpers
python
import numpy as np
virat = np.array([72, 45, 88, 31])
rohit = np.array([56, 0, 74, 88])
print(np.vstack((virat, rohit)))
print(np.vstack((virat, rohit)).shape)
print(np.hstack((virat, rohit)))
print(np.column_stack((virat, rohit)))text
[[72 45 88 31]
[56 0 74 88]]
(2, 4)
[72 45 88 31 56 0 74 88]
[[72 56]
[45 0]
[88 74]
[31 88]]How it works:
vstackturned each 1D array into a row → a 2 × 4 table (players × matches).hstackon 1D arrays just made one long 1D array of 8 scores.column_stackturned each 1D array into a column → a 4 × 2 table (matches × players).
Example — add a row and a column to an existing table
python
squad = np.array([[72, 45, 88, 31],
[56, 0, 74, 88]])
hardik = np.array([45, 32, 0, 67])
print(np.vstack([squad, hardik])) # new row
match5 = np.array([64, 32])
print(np.column_stack([squad, match5])) # new columntext
[[72 45 88 31]
[56 0 74 88]
[45 32 0 67]]
[[72 45 88 31 64]
[56 0 74 88 32]]How it works: vstack accepted a 1D row next to a 2D table (it reshapes for you). column_stack turned match5 into a (2, 1) column and attached it.
Why not hstack for the column?
python
np.hstack([squad, match5])text
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)hstack does not promote 1D to a column. Fix: np.hstack([squad, match5.reshape(-1, 1)]) — or just use column_stack.
Example — build a stats table from separate lists
python
runs = np.array([72, 45, 88])
balls = np.array([48, 32, 55])
print(np.column_stack((runs, balls)))text
[[72 48]
[45 32]
[88 55]]Each row is now one innings: [runs, balls]. (The same result as np.vstack((runs, balls)).T.)
Comparison — which one do I need?
| I have… | I want… | Use |
|---|---|---|
| two tables with the same columns | more rows | vstack / concatenate(axis=0) |
| two tables with the same rows | more columns | hstack / concatenate(axis=1) |
| several 1D arrays | each as a row | vstack |
| several 1D arrays | each as a column | column_stack |
| several 1D arrays | one long 1D array | hstack / concatenate |
| same-shape arrays | a brand-new axis | stack (next topic) |
Note:
np.row_stackis an old alias ofvstack. In NumPy 2 calling it emits a DeprecationWarning ("row_stack alias is deprecated. Use np.vstack directly.") — always writevstack.
Common mistakes
- ❌
np.hstack((runs, balls))expecting two columns. ✅ That gives one long 1D array — usenp.column_stack. - ❌
np.vstack(a, b)with two separate arguments. ✅np.vstack((a, b))— one tuple. - ❌ vstacking rows of different lengths. ✅ Every row must have the same number of columns.
- ❌ Using
np.row_stack. ✅ Usenp.vstack.
Interview tip
Q: Given 1D arrays x and y of length n, how do you make an (n, 2) array of points?
A: np.column_stack((x, y)) — or np.stack((x, y), axis=1), or np.vstack((x, y)).T. All three give the same (n, 2) result.
Key points to remember
- vstack stacks vertically (adds rows) and treats 1D arrays as rows of shape (1, n).
- hstack stacks horizontally (adds columns) for 2D arrays, but simply joins 1D arrays end to end.
- column_stack turns 1D arrays into columns — perfect for building a table from separate lists.
- vstack needs matching column counts; hstack/column_stack need matching row counts.
- hstack does not promote a 1D array to a column — reshape(-1, 1) it or use column_stack.
- np.row_stack is a deprecated alias in NumPy 2 — always use np.vstack.
Try it yourself
vstack vs hstack vs column_stack
Grow a squad table: new row and new column
Practice: build a batting card
Revision Flashcards
Tap a card to flip it and see the answer.
