Scalar selection

Scalar selection is the simplest method to select elements from an array and is implemented using [rowindex] for one-dimensional arrays, [rowindex, columnindex] for two-dimensional arrays, and so on. The following is a simple code that shows an array element reference:

import numpy as np
x = np.array([[2.0,4,5,6], [1,3,5,9]])

x[1,2]
5.0

A pure scalar selection always returns a single element and not an array. The data type of the selected element matches the data type of the array used in the selection. Scalar selection can also be used to assign a value to an array element, as shown in the following code:

x[1,2] = 8

x
array([[2, 4, 5, 6],[1, 3, 8, 9]])

Get Python: Data Analytics and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.