Selecting array elements
NumPy arrays can have their elements accessed via the []
operator. There are many variants of this operator that we will see throughout this book, but the basic access to array elements is by passing the zero-based offset of the desired element:
In [24]: # select 0-based elements 0 and 2 a1[0], a1[2] Out[24]: (0, 2)
Elements in a two-dimensional array can be used by making use of two values separated by a comma, with the row first and column second:
In [25]: # select an element in 2d array at row 1 column 2 m[1, 2] Out[25]: 6
It is possible to retrieve an entire row of a two-dimensional array using just a single value representing the row and omitting the column component:
In [26]: # all items in row 1 m[1,] Out[26]: ...
Get Learning pandas 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.