Shape, reshaping and axes
Last updated
A table of sales is a grid: stores down the side, days across the top. In NumPy that's a 2-D array, and its shape says how many rows and columns it has. In this lesson you'll change shapes, join arrays together, and learn the idea that most of NumPy and pandas depends on: working along an axis.
By the end of this lesson you can
- Read an array's
shape,ndimandsize - Change a shape with
reshape,raveland.T - Join arrays with
stackandconcatenate - Say which way
axis=0andaxis=1work on a table
Shape, ndim and size
Every array has three numbers that describe it:
shape: how long it is along each axis (each direction).(12,)is one row of 12;(3, 4)is 3 rows of 4.ndim: how many axes. A row is 1-D, a table 2-D.size: how many numbers in all. For(3, 4), 3 × 4 = 12.
Notice the comma in (12,). It's a tuple with one item, the way Python writes one. You'll see it often.
Reshaping
reshape arranges the same numbers in a new shape. Step through, then run the cells.
a = np.arange(12): twelve numbers in one row. Its shape is (12,): one axis, 12 long.Two more in the notebook:
g.ravel()flattens any shape back into one row.g.Ttransposes: rows become columns. A(3, 4)becomes a(4, 3). You'll use it when data comes with stores across the top and you want them down the side.
The numbers always keep their order, reading along each row in turn. Reshaping never sorts or moves them.
Stacking and splitting
Data often arrives in pieces: Monday's kilos for three stores, then Tuesday's. Run the cells to join them.
np.stack([mon, tue])puts each array on a new row: 2 days × 3 stores.np.column_stack([mon, tue])puts each one in a new column: 3 stores × 2 days. Usually the one you want, with one row per store.np.concatenate([mon, tue])joins them end to end, into one longer row.np.splitgoes the other way: one array into equal pieces.
What axis means
This is the idea to get right, because pandas uses it too.
(3, 4): axis 0 runs down the rows, axis 1 across the columns.axis=0 works down the rows, so you get one answer per column: the total for each day. axis=1 works across the columns: one answer per store. With no axis at all, every number goes into one answer, 422.
If you can't remember which is which, use the rule from the last frame: the axis you name is the one that disappears. Try sales.mean(axis=1) in a new cell, and say what it means before you run it.
Wrong way: twelve into fifteen
Run it.
cannot reshape array of size 12 into shape (5,3). A (5, 3) needs 15 numbers and there are 12. Reshaping never adds or drops numbers, so the sizes must match: the shape's numbers, multiplied, must equal size. 12 can be (3, 4), (4, 3), (2, 6), (6, 2), (2, 2, 3) and so on. Fix the cell with one of those.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: the absolute basics for beginners, shape and size: ndim, size and shape
- NumPy: numpy.reshape: reshaping, and -1 for “work it out”
- NumPy: numpy.stack: joining arrays along a new axis
- NumPy: Glossary, axis: what an axis is