O'Reilly logo

Mastering Python Data Visualization by Kirthi Raman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required