Quick Success Data Science
Slicing and dicing like a pro
NumPy is Python’s foundational library for numerical calculations. With NumPy, the heavy lifting is handled by arrays, essentially tables of elements of the same data type. Arrays are optimized for performance, permitting faster mathematical and logical operations than traditional Python data types, like lists.
In Part 1, we covered how to create arrays, describe them, and access their attributes using dot notation. In this article, we’ll examine how to access the elements in arrays using indexes and slices, so you can extract the value of elements and change them using assignment statements. Array indexing uses square brackets []
, just like Python lists.
As a refresher from Part 1, here is a graphical representation of a 1D, 2D, and 3D array, with the axes annotated. You’ll need to understand the axes’ directions to index properly.