Python NumPy - Indexing and Slicing Arrays

Just like lists, individual elements and slices of arrays can be selected using bracket notation. Unlike lists, however, arrays also permit selection using other arrays. That is, we can use array selectors to filter for specific subsets of elements of other arrays.

First, let's look at some examples of one-dimensional arrays that behave just like lists. Let's create an array called myarray with 10 values.

>>> import numpy as np
>>> myarray = np.arange(10)
>>> myarray
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>

As you can see, for arrays, indexing starts with 0. We have a few examples of selecting values below. In the first example, we select a single value at index 5 (6th value in the array). In the second example, we select all the elements of the array starting with index 2. In the third example,we specify the index range as 2 to 6. This will select all elements starting from index 2 upto but not including index 6. In the last example, we select all values except the last (-1).

>>> myarray[5]
5
>>> myarray[2:]
array([2, 3, 4, 5, 6, 7, 8, 9])
>>> myarray[2:6]
array([2, 3, 4, 5])
>>> myarray[:-1]
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>>

Slicing is possible over all dimensions. Let's look at some examples of slicing in a two-dimensional array. Let's say we have the following 2-d array:

>>> array2d = np.array([[10,20,30],[40,50,60]])
>>> array2d
array([[10, 20, 30],
       [40, 50, 60]])
>>>

Following are a few examples of slicing this array:

>>> array2d[1,1]
50
>>> array2d[0,2]
30
>>>

Data Assignment

We can assign values to slices of arrays as follows:

>>> myarray
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> myarray[2:6] = 100
>>> myarray
array([  0,   1, 100, 100, 100, 100,   6,   7,   8,   9])
>>>

As you can see, if you assign a scalar value to a slice, as in myarr[2:6] = 100, the value is broadcasted to the entire selection. An important distinction from lists is that array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array.

Exercises

You would notice that some concepts in the exercises below are not taught. Feel free to google or ask in forums.

  • Create an array with 50 elements. Then create another array which is a reverse of the first array.
  • Create an array with 10 random values and then sort it.
  • Create a 5x5 matrix using arrange and reshape functions. Then select all the elements from the third row.
  • Create a null vector of size 10 but the fourth value which is 1.

You may find these interesting

Single Index Model
The Single Index Model (SIM) is an asset pricing model, according to which the returns on a security...
Omega Index
The Omega Index (or Omega ratio) was developed by Keating and Shadwick in 2002. It is a ratio of the...
Inflation-Indexed Bonds
As the name suggests, in inflation-indexed bonds, the interest and principal payments are indexed to...

Related Downloads

Finance Train Premium
Accelerate your finance career with cutting-edge data skills.
Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.
I WANT TO JOIN
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Saylient AI Logo

Accelerate your finance career with cutting-edge data skills.

Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.