How to do NumPy 2-D array slicing & element access?

Numpy

In this article, we will learn NumPy array slicing and how we can access the elements of a 2-D array in NumPy. The concept of slicing is to take out a piece of information from an array based on indexes.

We will learn how we can get sub-arrays from a huge NumPy array by using the slicing concept. The use case of this concept in practical applications is when we have a huge data array and we want to process only a chunk of data out of this huge data set. So let us begin with our program.

We have an array of Indian cricket team players and we have their averages of ODI and Test career.We can print this array and confirm the elements of the array.

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Players details:",players)

Output:

Players details: [['Sachin' 'Dravid' 'Kohli' 'Dhoni']
['63' '54' '40' '49']
['71' '61' '49' '42']]

How to Access element of NumPy 2-D array


Now we will learn how to access the desired elements of the array.

Access rows of array

  • We can access and print the first row which has player names by using 0 indexes as:
  • Similarly, We can print the second row which has an ODI average by using index 1.
  • Finally, We can print the third row which has the Test average by using index 2.
import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Players Names:",players[0])

print("Players ODI average:",players[1])
print("Players Test average:",players[2])

Output:

Players Names: ['Sachin' 'Dravid' 'Kohli' 'Dhoni']
Players ODI average: ['63' '54' '40' '49']
Players Test average: ['71' '61' '49' '42']

Access 2 D array Row and columns

We can access and print the first row and second column which have Player ‘Dravid’ as an element. To do that we will need to use row index as 0 and column index as 1.

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Player Name:",players[0,1])

Output:

Player Name: Dravid


How to do 2D Array Slicing


We can do the slicing of the 2D array by taking out the intersection of rows and columns.The syntax for that will be like:

Syntax for 2D array Slicing

players[x:y: a:b]

Parameters in above syntax

  • x:y: These represent the range of rows to select
  • a:b: These represent the range of columns to select
import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Row 0:2 and Column 2:4",players[0:3,1:3])

Output:

Row 0:2 and Column 2:4 [['Dravid' 'Kohli']['54' '40']['61' '49']]

Slicing 1st column and all Rows

We can select all rows and 1st column by [ :,1:2]; will select all rows and 1:2 will select 1st column

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("All Row and 1st Column",players[:,1:2])

Output:

All Row and 1st Column [['Dravid'] ['54']['61']]

Slicing All rows ,1st and 2nd columns

In this example we are accessing All rows and 1st and 2nd column.

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("All Row and 1st & 2nd Column",players[:,1:3])

Output:

All Row and 1st & 2nd Column [['Dravid' 'Kohli']
 ['54' '40']
 ['61' '49']]

Access All columns and 0th and 1st using slice

In this example we are accessing All columns and 0th and 1st column by using slicing.

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("All column and 0 & 1st Rows",players[0:2,:])

Output:

All column and 0 & 1st Rows [['Sachin' 'Dravid' 'Kohli' 'Dhoni']
['63' '54' '40' '49']]

Access all rows and columns

We can select All rows and columns by using slicing

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("All column and All Rows",players[:])

Output:

All column and All Rows [['Sachin' 'Dravid' 'Kohli' 'Dhoni']
 ['63' '54' '40' '49']
 ['71' '61' '49' '42']]

Access negative Index using slicing

We can use negative index to find last row

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Only last Rows",players[-1,:])

Output:

Only last Rows ['71' '61' '49' '42']

Access Desired rows and columns

We can use negative indexes also to take desired rows and columns.

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Column and Rows in reverse",players[-3:-1,-2:])

Output:

Column and Rows in reverse [['Kohli' 'Dhoni']
['40' '49']]

Dots or ellipsis to access row and Columns


dots or ellipsis(. . .) can be used to select all columns or all rows. when we specify … it picks all rows or columns present in the array. To print all elements of row 0 we will use below code:

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Using … to display row 0 ",players[0,...] )

Output:

Using … to display row 0 ['Sachin' 'Dravid' 'Kohli' 'Dhoni']

Access 1st column using dots or ellipsis(. . .)

To print all elements of 1st column we will use below code:

import numpy as np
players = np.array([['Sachin','Dravid','Kohli','Dhoni'],[63,54,40,49],[71,61,49,42]])
print("Using … to display 1st column :",players[...,1] )

Output:

Using … to display1 ['Dravid' '54' '61']