Categories: NumpyPython

Indexing and slicing Numpy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

When an Numpy array is created, its elements can be returned by indexing. Indexing of array elements is implemented by using brackets. If there are several non contiguous elements to be indexed, a list of indices should be appended. Similar as for Python list, negative indexing represents indexing number from the end of an Numpy array. If you want to return parts of an array and assign to a new array, this is called slicing. While slicing a Python list is a copy of the corresponding part of a list, slicing of Numpy array is the view of the original array, which is quite different. Next code examples show working with indexing and slicing of Numpy arrays.

#Import Numpy module
import numpy as np
#create an one-dimensional array of length 10
x = np.arange(1, 11)
x
#output
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
#return the 9th element
x[8]
#Out 
9
#Indexing using negative symbol
#the last element of Numpy array
x[-1]
#Out
10
#the third last element of an array
x[-3]
#Out 
8 
#Using colon for contiguous indexing
#return second to fourth elements of array
x[1:4]
#Output
array([2, 3, 4])
#non contiguous indexing of array
x[[5, 7, 9]]
#Out
array([ 6,  8, 10])
#a two-dimensional array of shape (2,4)
B = np.arange(1, 9).reshape((2, 4))
B
#output
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
#indexing the element at second row, fourth column of array
B[1, 3]
#Output
8
#Create an array of shape(3,3)
C = np.arange(1, 10).reshape((3, 3))
C
#Output 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
#return second row all column of array
C[1, :]
#result
array([4, 5, 6])
#return all rows, third column of array
C[:, 2]
#Output
array([3, 6, 9])
#return first three rows, and first and second columns
C[0:3, 0:2]
#Output
array([[1, 2],
       [4, 5],
       [7, 8]])
#return first and second rows, first and second columns of array
C[[1, 2], 1:3]
#Output 
array([[5, 6],
       [8, 9]])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago