We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

NumPy – Select Rows / Columns By Index

In this article we will discuss how to select elements from a 2D Numpy Array . Elements to select can be a an element only or single/multiple rows & columns or an another sub 2D array.

First of all, let’s import numpy module i.e.

Advertisements

import numpy as np

Now let’s create a 2d Numpy Array by passing a list of lists to numpy.array() i.e.

# Create a 2D Numpy adArray with 3 rows & 3 columns | Matrix
nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

Contents of the 2D Numpy Array will be,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Now let’s see how to select elements from this 2D Numpy Array by index i.e.

Select a single element from 2D Numpy Array by index

We can use [][] operator to select an element from Numpy Array i.e.

ndArray[row_index][column_index]

Example 1:

Select the element at row index 1 and column index 2.

# Select element at row index 1 & column index 2
num = nArr2D[1][2]

print('element at row index 1 & column index 2 is : ' , num)

Output:

element at row index 1 & column index 2 is :  33

Example 2:

Or we can pass the comma separated list of indices representing row index & column index too i.e.

# Another way to select element at row index 1 & column index 2
num = nArr2D[1, 2]

print('element at row index 1 & column index 2 is : ', num)

Output:

element at row index 1 & column index 2 is :  33

Select Rows by Index from a 2D Numpy Array

We can call [] operator to select a single or multiple row. To select a single row use,

ndArray[row_index]

It will return a complete row at given index.

To select multiple rows use,

ndArray[start_index: end_index ,  :]

It will return rows from start_index to end_index – 1 and will include all columns.

Let’s use this,

Contents of the 2D a Numpy Array nArr2D created above are,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Let’s select a row at index 2 i.e.

# Select a Row at index 1
row = nArr2D[1]

print('Contents of Row at Index 1 : ' , row)

Output:

Contents of Row at Index 1 :  [11 22 33]

Select multiple rows from index 1 to 2 i.e.

# Select multiple rows from index 1 to 2
rows = nArr2D[1:3, :]

print('Rows from Index 1 to 2 :')
print(rows)

Output:

Rows from Index 1 to 2 :
[[11 22 33]
 [43 77 89]]

Select multiple rows from index 1 to last index

# Select multiple rows from index 1 to last index
rows = nArr2D[1: , :]

print('Rows from Index 1 to last row :')
print(rows)

Output:

[[11 22 33]
 [43 77 89]]

Select Columns by Index from a 2D Numpy Array

To select a single column use,

ndArray[ : , column_index]

It will return a complete column at given index.

To select multiple columns use,

ndArray[ : , start_index: end_index]

It will return columns from start_index to end_index – 1.

Let’s use these,

Contents of the 2D Numpy Array nArr2D created above are,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Select a column at index 1

# Select a column at index 1
column = nArr2D[:, 1]

print('Contents of Column at Index 1 : ', column)

Output:

Contents of Column at Index 1 :  [22 22 77]

Select multiple columns from index 1 to 2

# Select multiple columns from index 1 to 2
columns = nArr2D[: , 1:3]

print('Column from Index 1 to 2 :')
print(columns)

Output:

Column from Index 1 to 2 :
[[22 23]
 [22 33]
 [77 89]]

Select multiple columns from index 1 to last index

# Select multiple columns from index 1 to last index
columns = nArr2D[:, 1:]

Output is same as above because there are only 3 columns 0,1,2. So 1 to last columns means columns at index 1 & 2.

Select a Sub Matrix or 2d Numpy Array from another 2D Numpy Array

To select sub 2d Numpy Array we can pass the row & column index range in [] operator i.e.

ndArray[start_row_index : end_row_index , start_column_index : end_column_index]

It will return a sub 2D Numpy Array for given row and column range.

Let’s use these,

Contents of the 2D Numpy Array nArr2D created at start of article are,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Select a sub 2D Numpy Array from row indices 1 to 2 & column indices 1 to 2

# Select a sub 2D array from row indices 1 to 2 & column indices 1 to 2
sub2DArr = nArr2D[1:3, 1:3]

print('Sub 2d Array :')
print(sub2DArr)

Output:

Sub 2d Array :
[[22 33]
 [77 89]]

Selected Row or Column or Sub Array is View only

Contents of the Numpy Array selected using [] operator returns a View only i.e. any modification in returned sub array will be reflected in original Numpy Array .
Let’s check this,

Contents of the 2D Numpy Array nArr2D created at start are,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Select a row at index 1 from 2D array i.e.

# Select row at index 1 from 2D array
row = nArr2D[1]

Contents of row : 

[11 22 33]

Now modify the contents of row i.e.

# Change all the elements in selected sub array to 100
row[:] = 100

New contents of the row will be

[100 100 100]

Modification in sub array will be reflected in main Numpy Array too. Updated Contents of the 2D Numpy Array nArr2D are,

[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]

Get a copy of 2D Sub Array from 2D Numpy Array using ndarray.copy()

to the copy instead of view in sub array use copy() function.
Let’s check this,

Create a 2D Numpy adArray with3 rows & columns | Matrix

# Create a 2D Numpy adArray with3 rows & columns | Matrix
nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

Content of nArr2D is,

[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]

Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 100

# Select a copy of row at index 1 from 2D array
row = nArr2D[1].copy()

# Set all the elements in selected sub array to 100
row[:] = 100

Here, sub array is a copy of original array so, modifying it will not affect the original Numpy Array
Contents of the modified sub array row is,

[100 100 100]

Contents of the original Numpy Array is,

[[21 22 23]
 [11 22 33]
 [43 77 89]]

Complete example is as follows,

import numpy as np


def main():
   # Create a 2D Numpy adArray with 3 rows & 3 columns | Matrix
   nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

   print('Contents of 2D Array : ')
   print(nArr2D)

   print('*** Select an element by index from a 2D ndArray')

   # Select element at row index 1 & column index 2
   num = nArr2D[1][2]

   print('element at row index 1 & column index 2 is : ' , num)

   # Another way to select element at row index 1 & column index 2
   num = nArr2D[1, 2]

   print('element at row index 1 & column index 2 is : ', num)


   print('*** Select Rows by Index from a 2D ndArray ***')

   # Select a Row at index 1
   row = nArr2D[1]

   print('Contents of Row at Index 1 : ' , row)

   # Select multiple rows from index 1 to 2
   rows = nArr2D[1:3, :]

   print('Rows from Index 1 to 2 :')
   print(rows)

   # Select multiple rows from index 1 to last index
   rows = nArr2D[1: , :]
   print('Rows from Index 1 to last row :')
   print(rows)

   print('*** Select Columns by Index from a 2D ndArray ***')

   # Select a column at index 1
   column = nArr2D[:, 1]

   print('Contents of Column at Index 1 : ', column)

   # Select multiple columns from index 1 to 2
   columns = nArr2D[: , 1:3]

   print('Column from Index 1 to 2 :')
   print(columns)

   # Select multiple columns from index 1 to last index
   columns = nArr2D[:, 1:]

   print('Column from Index 1 to last index :')
   print(columns)

   print('*** Select a Sub Matrix or 2d Array from another 2D ndArray ***')

   print('Original ndArray')
   print(nArr2D)

   # Select a sub 2D array from row indices 1 to 2 & column indices 1 to 2
   sub2DArr = nArr2D[1:3, 1:3]

   print('Sub 2d Array :')
   print(sub2DArr)



   print('*** Sub Array is View only ***')

   print('Original ndArray')
   print(nArr2D)

   # Select row at index 1 from 2D array
   row = nArr2D[1]

   print('Contents of row / sub array')
   print(row)

   # Change all the elements in selected sub array to 100
   row[:] = 100

   # As sub array is a copy so, changes in it will be reflected in original array too

   print('Contents of modified row / sub array')
   print(row)
   print('Original ndArray')
   print(nArr2D)

   print('*** Fetching a copy of 2D Sub Array from 2D ndArray ***')

   # Create a 2D Numpy adArray with3 rows & columns | Matrix
   nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

   # Select a copy of row at index 1 from 2D array
   row = nArr2D[1].copy()

   # Set all the elements in selected sub array to 100
   row[:] = 100

   '''
   Here sub array is a copy of original array so, modifying it will not affect the original ndArray
   '''

   print('Contents of modified row / sub array')
   print(row)
   print('Original ndArray')
   print(nArr2D)



if __name__ == '__main__':
   main()

Output:

Contents of 2D Array : 
[[21 22 23]
 [11 22 33]
 [43 77 89]]
*** Select an element by index from a 2D ndArray
element at row index 1 & column index 2 is :  33
element at row index 1 & column index 2 is :  33
*** Select Rows by Index from a 2D ndArray ***
Contents of Row at Index 1 :  [11 22 33]
Rows from Index 1 to 2 :
[[11 22 33]
 [43 77 89]]
Rows from Index 1 to last row :
[[11 22 33]
 [43 77 89]]
*** Select Columns by Index from a 2D ndArray ***
Contents of Column at Index 1 :  [22 22 77]
Column from Index 1 to 2 :
[[22 23]
 [22 33]
 [77 89]]
Column from Index 1 to last index :
[[22 23]
 [22 33]
 [77 89]]
*** Select a Sub Matrix or 2d Array from another 2D ndArray ***
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]
Sub 2d Array :
[[22 33]
 [77 89]]
*** Sub Array is View only ***
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]
Contents of row / sub array
[11 22 33]
Contents of modified row / sub array
[100 100 100]
Original ndArray
[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]
*** Fetching a copy of 2D Sub Array from 2D ndArray ***
Contents of modified row / sub array
[100 100 100]
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]

 

Cart

Your Cart is Empty

Back To Shop