NumPy · lesson 4 of 13
Parts in this lesson
Aarr.shaperows, columns, and more
Barr.reshape(3, 4)the same numbers, a new shape
Carr.sum(axis=0)work down the rows

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, ndim and size
  • Change a shape with reshape, ravel and .T
  • Join arrays with stack and concatenate
  • Say which way axis=0 and axis=1 work 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.

Twelve numbers, three shapes. a = np.arange(12): twelve numbers in one row. Its shape is (12,): one axis, 12 long.01234567891011shape (12,)
1 of 4 a = np.arange(12): twelve numbers in one row. Its shape is (12,): one axis, 12 long.
shape.ipynb

Two more in the notebook:

  • g.ravel() flattens any shape back into one row.
  • g.T transposes: 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.

stack.ipynb
  • 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.split goes the other way: one array into equal pieces.

What axis means

This is the idea to get right, because pandas uses it too.

Summing down and across. Kilos sold by 3 stores (rows) over 4 days (columns). Shape (3, 4): axis 0 runs down the rows, axis 1 across the columns.423845403533313729303428sales: 3 stores × 4 daysMonTueWedThuBatangasLipaTanauan
1 of 4 Kilos sold by 3 stores (rows) over 4 days (columns). Shape (3, 4): axis 0 runs down the rows, axis 1 across the columns.
axis.ipynb

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.

reshape.ipynb

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

An array has shape (7, 5). What shape does .sum(axis=0) give?
Which reshape works on an array of 18 numbers?
sales has stores as rows and days as columns. You want each store’s best day’s kilos. What do you write?

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.

All sources for Simple Data Analysis