The axis Parameter Explained

The axis parameter tells a NumPy function which dimension to operate along and collapse — axis=0 works down the rows to give a result per column, while axis=1 works across the columns to give a result per row.

Learn The axis Parameter Explained in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll build a reliable mental model using sum and mean on 2D arrays, learn the one rule that ends the confusion forever, and see how keepdims keeps shapes broadcast-friendly.

Forget "rows" and "columns" for a moment. The reliable rule is: the axis you pass is the one that gets collapsed and removed from the result's shape. A (3, 4) array reduced along axis=0 loses the 3 and becomes shape (4,) ; reduced along axis=1 it loses the 4 and becomes shape (3,) .

The same idea works for every reduction — mean , max , min , std , and more. Think of a small grades table where rows are students and columns are subjects: axis=0 gives the class average per subject , while axis=1 gives each student's average across subjects .

Normally a reduction removes the axis entirely, dropping a dimension. Passing keepdims=True keeps that axis with size 1, so the result lines up perfectly for broadcasting back onto the original array — the standard way to subtract per-row or per-column means.

Replace ___ with the correct axis number so you get one total per column .

Answer: 0 — collapsing axis 0 (the rows) leaves one total per column.

It collapses the row dimension, giving a per-column result:

✅ Fix: say "the named axis disappears" — axis=0 leaves a result per column.

❌ "operands could not be broadcast" after a reduction

✅ Fix: add keepdims=True so the result is (3, 1) and broadcasts.

✅ Fix: check arr.ndim ; valid axes run from 0 to ndim − 1.

Given a temperatures table (rows = cities, columns = days), find the warmest day overall per city and the average temperature per day across cities.

Lesson complete — axis confusion solved!

You now know that the axis you name is the one that collapses: axis=0 gives per-column results, axis=1 gives per-row results, and keepdims=True keeps shapes broadcast-ready.

🚀 Up next: Fancy Indexing — select and reorder elements with arrays of integer positions.

Practice quiz

What is the one reliable rule about the axis you pass to a reduction?

  • It is the axis you keep
  • It is always the rows
  • It is the axis that gets collapsed and removed
  • It must be 1 for 2D arrays

Answer: It is the axis that gets collapsed and removed. The named axis disappears from the result's shape.

A (3, 4) array reduced along axis=0 becomes which shape?

  • (3,)
  • (4,)
  • (3, 4)
  • (1, 4)

Answer: (4,). axis=0 loses the 3, leaving shape (4,).

A (3, 4) array reduced along axis=1 becomes which shape?

  • (3,)
  • (4,)
  • (3, 4)
  • (1, 3)

Answer: (3,). axis=1 loses the 4, leaving shape (3,).

For grades = [[80,90,100],[60,70,80]], what is grades.mean(axis=0)?

axis=0 collapses the students, giving the per-subject averages [70. 80. 90.].

For grades = [[80,90,100],[60,70,80]], what is grades.mean(axis=1)?

axis=1 collapses the subjects, giving each student's average: 90 and 70.

What does keepdims=True do during a reduction?

  • Doubles the result
  • Removes one extra axis
  • Keeps the reduced axis with size 1
  • Returns a scalar

Answer: Keeps the reduced axis with size 1. keepdims=True keeps the collapsed axis as size 1 for easy broadcasting.

For grades = [[80,90,100],[60,70,80]], what is grades.max(axis=0)?

axis=0 collapses rows, giving the highest score in each subject column.

Which call returns a single scalar over the entire array?

  • arr.sum(axis=0)
  • arr.sum(axis=1)
  • arr.sum(keepdims=True)
  • arr.sum()

Answer: arr.sum(). With no axis, the reduction runs over the whole flattened array.

A (3, 4) array reduced along axis=1 with keepdims=True becomes which shape?

  • (3,)
  • (3, 1)
  • (1, 4)
  • (4,)

Answer: (3, 1). keepdims keeps the collapsed axis at size 1, giving (3, 1).

Reading axis=0 as 'sum the rows you see' is wrong because...

  • axis=0 errors on 2D arrays
  • it sums diagonals
  • it actually gives a per-column result
  • it returns a scalar

Answer: it actually gives a per-column result. axis=0 collapses the row dimension, producing one value per column.