NumPy · lesson 5 of 13
Parts in this lesson
Aa[2:5]a slice: start, up to but not including stop
Bg[:, 2]every row, column 2
Ca[a > 45]a mask: the elements where it’s True
D(a > 40) & (a < 60)both conditions

Indexing, slicing and masks

Last updated

Most questions about data start with picking part of it: Tuesday's sales, the last three days, every day over 45 kg. In this lesson you'll pick parts of arrays by position, by slice and by condition. The last of those, the boolean mask, is how you'll filter data for the rest of the course.

By the end of this lesson you can

  • Pick elements, rows, columns and blocks by position and slice
  • Filter an array with a boolean mask
  • Combine conditions with & and |, and say why and fails
  • Choose between two values with np.where

Positions and slices

Slicing an array works like slicing a list or a string, with one addition: in 2-D you give the rows and the columns, separated by a comma. Step through the picture, then run the cells.

Which cells each index picks. a = np.arange(0, 100, 10). Positions start at 0. a[3] is one number: 30.0102030405060708090a[3]0123456789
1 of 8 a = np.arange(0, 100, 10). Positions start at 0. a[3] is one number: 30.
slices.ipynb

The rules, all from Python:

  • Positions start at 0. Negative positions count from the end: -1 is the last.
  • A slice start:stop:step includes start and stops before stop. Leave a part out to mean "from the beginning", "to the end" or "every one".
  • In 2-D, g[rows, columns]. A bare : means "all of them", so g[:, 2] is a whole column and g[1, :], or just g[1], is a whole row.

Masks

Positions are useful when you know where things are. Usually you don't: you want "the days over 45 kg", wherever they are. That's a mask. Run the cells.

masks.ipynb
  • kilos > 45 compares every element and gives an array of True and False, the same shape. That's the mask.
  • kilos[mask] keeps the elements where the mask is True: [51, 66, 59].
  • For two conditions, use & (and) and | (or), with brackets round each condition. Without the brackets, Python would work out 40 & kilos first.
  • kilos[[0, 6]] picks by a list of positions: the first and last day. This is called fancy indexing.
  • np.where(condition, a, b) gives a where the condition is true and b where it isn't: a label for every day.

Try kilos[kilos < kilos.mean()] in a new cell: the days below the week's average.

Wrong way: and instead of &

In plain Python you write and. Try it on arrays.

and.ipynb

The truth value of an array with more than one element is ambiguous. Python's and asks "is the left side true?", one yes or no for the whole thing. But kilos > 40 is seven answers, some True and some False, so there's no single answer, and NumPy refuses to guess.

& compares element by element instead, which is what you meant. Change and to & and run it again. The same goes for or (use |) and not (use ~). You'll see this error again in pandas, for the same reason.

Check what you learned

a = np.arange(0, 100, 10). What is a[1:4]?
g is a table with stores as rows. Which picks the last store’s row?
You want the days that were under 40 kg or over 60 kg. 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