In this post, we will learn about indexing in python.
Indexing of 1D array:
Ex: Create an array of numbers
>> var = np.arange(1,100,10)
>> var
Output:
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
In the output, we have a total of 10 elements.
Now let's learn how to access these elements....
These elements in the array can be accessed using an index inside the square brackets i.e., [index]. The index is like an address in the memory, where the values of the array get stored. Forward indexing starts with 0 and inverse indexing starts with -1(Check the picture presented at the start of the page). In forward indexing, the elements of the array can be accessed starting from the first element of the array. Forward indexing starts from 0,1,2,3,... and in inverse indexing, the elements can be accessed from the last value of the array considering the last value as the first element. Inverse indexing starts from the last value in the array, in the form of -1,-2,-3, ...
Accessing the array elements using the index of the array.
Syntax:
arrayname[index]
Now count the position of 21 in the array (var is an array name) by starting the count with 0. Then 21 will have a count of 2, which is the index of 21. Use var[2] to get the result.
>> var[2]
Output:
21
Slicing: Accessing a range of values from an array
Ex2: Get a range of values from 1 to 61 through indexing.
From the table, we can get the index of the values in the array. The index of 1 is 0 and the Index of 61 is 6.
>>var[0:7]
Output:
array([ 1, 11, 21, 31, 41, 51, 61])
Remember the result will include the value that has a start index_number and exclude the values with stop index_number. The result will have values starting with index 0 and end with an index of 6.
Ex: To access the last element from the array var.
>> var [-1]
Output:
91
Here we accessed the last element of the array using the index of -1.
Ex: To access the last 3 elements from the array var.
>> var[-3:]
Output:
[71, 81, 91]
Manipulating the values in the array:
a) Setting the range of values in the array to a particular value
arrayname[range]= required value
Ex: Update the first 3 values in an array named var with 60.
>>var[ 0:3]=60
>> var
Output:
array([60, 60, 60, 31, 41, 51, 61, 71, 81, 91])
Ex: Update all the elements in the array named var with 100.
>>var[:]=100
>> var
Output:
array([100, 100, 100, 100, 100, 100, 100, 100, 100, 100])
Indexing of 2D array (matrices):
Syntax:
2D_arrayname[row][column]
or
2D_arrayname[row,column]
We can use any of these two formats. In general second format is more popular.
Lets create a simple 3x3 matrix array named as mat.
>>mat=np.array(([10,20,30],[40,50,60],[70,80,90]))
>>mat
Output:
array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])a) To access a single row
mat[row number] or mat[rownumber, :]
Here colon (:) indicates all the columns in the matrix
Ex: To access the first row in the matrix
>> mat[0]
Output:array([70, 80, 90])>> mat[0,:]
Output:array([70, 80, 90])Any of the two methods ca be used to access the rows of the matrix.
b) To access multiple rows of the matrix
Ex: To access the first two rows of the matrix
>> mat[0:2,:]
Output:array([[10, 20, 30], [40, 50, 60]])c) To access a single column
mat[row number, column number] or mat[rownumber , :]
Here colon(:) indicates all the rows in the matrix
Ex: To access first column from the matrix
>> mat[:,0]
Output:array([10, 40, 70])d) Accessing multiple columns of a matrix
Ex: Accessing the last two columns
>> mat[:,1:]
Output:array([[20, 30], [50, 60], [80, 90]])Ex: Accessing the first two columns
>> mat[:,0:2]
Output:array([[10, 20], [40, 50], [70, 80]])c) Accessing specific number of rows and columns
Ex: Accessing the first 2 rows of last column
>> mat[0:2,-1]
Output:array([30, 60])Ex: Accessing the last 2 rows of first column
>>mat[-2:,0]
Output:array([40, 70])Ex: Accessing first two rows for last two columns
>> mat[0:2,-2:]
Output:array([[20, 30], [50, 60]])Ex: Accessing first two rows for first two columns
>> mat[0:2,0:2]
Output:array([[10, 20], [40, 50]])Ex: Accessing second row and third column vaue
>> mat[1,2]
Output:60we will use 1 as the 2nd row or column will have an index value of 1.
Ex: Accessing the first and last rows of a matrix
>>mat[[0,2]]
Output:array([[10, 20, 30], [70, 80, 90]])d) Creating a copy of an array
>>mat_data= mat.copy()
>>mat_data
Output:array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])mat and mat_data will have same elements as the mat_data is the copy of the array mat.
Hope this helpful to understand indexing..
Keep learning!😊
Comments
Post a Comment