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.

Related Downloads

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $39 (Regular $57)
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.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.