# 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!

```
import numpy as np
np.__version__
```

#### 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:

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

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

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

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:

```
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]
```

If an index of the form `x:`

is used, all values past `x`

will be returned:

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

And building on top of that, a `start`

and `step`

can be specified with `end`

left blank:

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

#### 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.

```
array = np.array([['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']])
array[[1],
[2]]
```

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

```
array = np.array([['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']])
array[[0, 1, 0, 0, 2],
[1, 1, 0, 2, 1]]
```

#### Boolean Indexing¶

Indexing can also function with a Boolean statement:

```
array = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
array[array >= 4]
```