Show Sidebar Hide Sidebar

Indexing

The basics of indexing NumPy arrays.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Imports

This tutorial imports Numpy.

In [1]:
import numpy as np
np.__version__
Out[1]:
'1.11.2'

Basic Slicing and Indexing

Indexing a NumPy array is very similar to indexing a standard Python List. That is to say, most functionality of Python lists transfers over to NumPy arrays.

Single value indexing works the same way:

In [2]:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
array[12]
Out[2]:
12

A negative number as your index is interpreted as counting backwards from the end of the array:

In [3]:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
array[-2]
Out[3]:
16

Slicing also works the same way, similar to the principle behind how NumPy functions like np.linspace() construct an array based on start, stop and step parameters:

In [4]:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
array[1:14:2]
Out[4]:
array([ 1,  3,  5,  7,  9, 11, 13])

If an index of the form x: is used, all values past x will be returned:

In [5]:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
array[5:]
Out[5]:
array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17])

And building on top of that, a start and step can be specified with end left blank:

In [6]:
array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
array[5: :2]
Out[6]:
array([ 5,  7,  9, 11, 13, 15, 17])

Integer Array Indexing

You can index an ND-array with a index of the form:

$$ \begin{align*} [[r_1, ... , r_n], [c_1, ... , c_n]] \end{align*} $$

where $n$ can be any number. The values $r_1$ and $c_1$ respresent the row number and the column number to select from in order to pick a value to populate our new indexed array. For example, if $r_1$ is 1 and $c_1$ is 2, then we take the element in row 2 and column 3 of our original array and that will be our first value.

In [7]:
array = np.array([['a', 'b', 'c'],
                  ['d', 'e', 'f'],
                  ['g', 'h', 'i']])
array[[1],
      [2]]
Out[7]:
array(['f'], 
      dtype='|S1')

We can use a larger $n$ to index a larger array:

In [8]:
array = np.array([['a', 'b', 'c'],
                  ['d', 'e', 'f'],
                  ['g', 'h', 'i']])
array[[0, 1, 0, 0, 2],
      [1, 1, 0, 2, 1]]
Out[8]:
array(['b', 'e', 'a', 'c', 'h'], 
      dtype='|S1')

Boolean Indexing

Indexing can also function with a Boolean statement:

In [9]:
array = np.array([[0, 1, 2],
                  [3, 4, 5],
                  [6, 7, 8]])
array[array >= 4]
Out[9]:
array([4, 5, 6, 7, 8])
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.