Select value in array python

In this article we will discuss how to select an element or a sub array from a Numpy Array by index.

Let’s create a Numpy Array using numpy.arange()

# Create a numpy ndArray
npArray = np.arange(1, 20, 2)

print(npArray)

Contents of the Numpy Array is as follows,

[ 1  3  5  7  9 11 13 15 17 19]

Now let’s discuss how to select elements from this Numpy Array by index.

To select an element from Numpy Array , we can use [] operator i.e.

ndarray[index]

It will return the element at given index only.

Let’s use this to select an element at index 2 from Numpy Array we created above i.e. npArray,

# Select an element at index 2 (Index starts from 0)
elem = npArray[2]

print('Element at 2nd index  : ' , elem)

Output:

Element at 2nd index  :  5

Advertisements

Select a sub array from Numpy Array by index range

We can also select a sub array from Numpy Array using [] operator i.e.

ndArray[first:last]

It will return a sub array from original array with elements from index first to last – 1.

Let’s use this to select different sub arrays from original Numpy Array .

Contents of the original numpy  Numpy Array we created above i.e. npArray is as follows,

[ 1  3  5  7  9 11 13 15 17 19]

Now let’s see some examples,

Example 1: Select a sub array with elements from index 1 to 6,

# Select elements from index 1 to 6
subArray = npArray[1:7]

Contents of sub Array is as follows,

[ 3  5  7  9 11 13]

Example 2: Select elements from beginning to index 3

subArray = npArray[:4]

Output:

[1 3 5 7]

Example 3: Select elements from 2nd index to end

subArray = npArray[2 : ]

Output:

[ 5  7  9 11 13 15 17 19]

Sub Numpy Array is just a view | Broadcasting

Sub Numpy Array returned by [] operator is just a view of original array i.e. data is not copied just a sub view of original ndarray is created.
Any modification in it will be reflected in original Numpy Array too.

Let’s confirm this.

Create a Numpy Array ,

npArray = np.arange(1, 20, 2)

It’s Contents are,

[ 1  3  5  7  9 11 13 15 17 19]

select a sub array from it,

subArray = npArray[1:7]

Contents of sub array is ,

[ 3  5  7  9 11 13]

Modify the contents of sub array,

# Change contents of sub array
subArray[1] = 220

Sub array is just a view of original array i.e. data is not copied just a view of sub array is created. Any modification in it will be reflected in original Numpy Array too,

print('Contents of modified Sub Array : ', subArray)
print('Contents of Original Array : ', npArray)

Output:

Contents of modified Sub Array :  [  3 220   7   9  11  13]
Contents of Original Array :  [  1   3 220   7   9  11  13  15  17  19]

We modified the sub Numpy Array only but changes are reflected in original Numpy Array too.
In case of data analysis in data science we generally use Numpy Array with large data set, so to avoid unnecessary copy, ndarray added the feature of view only also called broadcasting.

Create a copy of Sub Array of Numpy Array

We can also create a copy of sub array using,

ndArray[index_range].copy()

It will return the copy of sub array.

Let’s see an example,

npArray = np.arange(1, 20, 2)
print('Contents of Original Array : ', subArray)

# Fetch a copy of sub array from index 1 to 6
subArray = npArray[1:7].copy()
print('Contents of Sub Array : ', subArray)

# Change contents of sub array
subArray[1] = 220

print('Contents of modified Sub Array : ', subArray)
print('Contents of Original Array : ', npArray)

Output:

Contents of Original Array :  [  3 220   7   9  11  13]
Contents of Sub Array :  [ 3  5  7  9 11 13]
Contents of modified Sub Array :  [  3 220   7   9  11  13]
Contents of Original Array :  [ 1  3  5  7  9 11 13 15 17 19]

As sub Array is a copy not the view only, so changes made in it will not be reflected in main array.

Complete example is as follows,

import numpy as np


def main():


   # Create a numpy ndArray
   npArray = np.arange(1, 20, 2)

   print('Contents of numpy ndArray')
   print(npArray)

   print('*** Select an element by Index ***')

   # Select an element at index 2 (Index starts from 0)
   elem = npArray[2]

   print('Element at 2nd index  : ' , elem)

   print('*** Select a by sub array by Index Range ***')

   # Select elements from index 1 to 6
   subArray = npArray[1:7]

   print('Sub Array from 1st to 6th index are :', subArray)

   # Select elements from beginning to index 3
   subArray = npArray[:4]

   print('Sub Array from beginning to 3rd index are :', subArray)

   # Select elements from 2nd index to end
   subArray = npArray[2:]

   print('Sub Array from 2nd index to end are :', subArray)


   print('*** Sub Array is just a View not the copy ***')


   npArray = np.arange(1, 20, 2)

   print('Contents of Original Array : ', subArray)

   # Select a sub array of elements from index 1 to 6
   subArray = npArray[1:7]

   print('Contents of Sub Array : ', subArray)

   # Change contents of sub array
   subArray[1] = 220
   '''
   Sub array is just a view of original array i.e. data is not copied just a view of sub array is created.
   Any modification in it will be reflected in original nodArray too
   '''
   print('Contents of modified Sub Array : ', subArray)
   print('Contents of Original Array : ', npArray)


   print('*** Create a copy of Sub Array of ndArray *** ')

   npArray = np.arange(1, 20, 2)
   print('Contents of Original Array : ', subArray)

   # Fetch a copy of sub array from index 1 to 6
   subArray = npArray[1:7].copy()
   print('Contents of Sub Array : ', subArray)

   # Change contents of sub array
   subArray[1] = 220

   '''
   As subArray is a copy of sub array not the view only, so changes made in it will not be reflected in main array.
   '''
   print('Contents of modified Sub Array : ', subArray)
   print('Contents of Original Array : ', npArray)


if __name__ == '__main__':
   main()

Output:

Contents of numpy ndArray
[ 1  3  5  7  9 11 13 15 17 19]
*** Select an element by Index ***
Element at 2nd index  :  5
*** Select a by sub array by Index Range ***
Sub Array from 1st to 6th index are : [ 3  5  7  9 11 13]
Sub Array from beginning to 3rd index are : [1 3 5 7]
Sub Array from 2nd index to end are : [ 5  7  9 11 13 15 17 19]
*** Sub Array is just a View not the copy ***
Contents of Original Array :  [ 5  7  9 11 13 15 17 19]
Contents of Sub Array :  [ 3  5  7  9 11 13]
Contents of modified Sub Array :  [  3 220   7   9  11  13]
Contents of Original Array :  [  1   3 220   7   9  11  13  15  17  19]
*** Create a copy of Sub Array of ndArray *** 
Contents of Original Array :  [  3 220   7   9  11  13]
Contents of Sub Array :  [ 3  5  7  9 11 13]
Contents of modified Sub Array :  [  3 220   7   9  11  13]
Contents of Original Array :  [ 1  3  5  7  9 11 13 15 17 19]

How do you select a value in an array in Python?

Use integer array indexing to select an element by index Use the syntax np. array[i,j] to retrieve an element at row index i and column index j from the array. To retrieve multiple elements, use the syntax np.

How do you select a value in an array?

You select a value from an array by referring to the index of its element. Array elements (the things inside your array), are numbered/indexed from 0 to length-1 of your array.

How do you access part of an array in Python?

An array in Python is used to store multiple values or items or elements of the same type in a single variable. We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created.

How do you copy part of an array in Python?

To create a deep copy of an array in Python, use the array. copy() method. The array. copy() method does not take any argument because it is called on the original array and returns the deep copied array.