NumPy is one of the most popular Python libraries used to perform various numerical calculations in the data analysis process. It is primarily used when working with arrays, and offers statistical and mathematical operations.

NumPy Arrays

Arrays are one of the fundamental data structures in the NumPy library. They are basically a multi-dimensional container used to store, access, manipulate, and locate numerical data. NumPy arrays are a better and faster alternative to Python lists.

To use NumPy, you first need to install the library:

pip install numpy
import numpy as np

The array() function can be used to create a NumPy array:

x = np.array([2,3,4,6])

Accessing a NumPy Array

In Python, accessing arrays makes it easier to extract and modify data. Accessing a specific element is also known as array indexing.

Basic attributes for NumPy arrays:

size: To determine the size of an array.

shape: To define an array's shape.

ndim: To specify the number of dimensions.

itemsize: To specify size of each element.

dtype: To determine the data types of elements.

The above array has 12 elements, is a 2-dimensional array (4,3), has 4 subarrays, and is an integer type (int32).

Accessing elements in NumPy Arrays

Indexing is used to access columns in NumPy arrays. Array indexing is one of the most useful processes in data analysis, as it allows us to extract specific data we’re interested in. There are numerous approaches to accomplish this, some of which are described below:

Using indexing and slicing:

Indexing: In this method, index numbers are used to extract individual elements from an array.

x = np.array([2, 3, 1, 5, 6])
x[0]        # 2
x[2]        # 1
x[-1]       # 6

In the above example, x[-1] denotes the last element of the array, which is 6.

Negative indexing means indexing starts from the end rather than the first element, so the last element in the array will have an index number of -1.