Indexing a 2D array

A 2D array is an array of arrays. In this, the position of the data element normally refers to two indices instead of one and it represents the table with rows and columns of data. Now we are going to do indexing of such a type of arrays.

So, let's look at an example of a 2D array:

>>> td_array = np.array(([5,6,7],[8,9,10],[11,12,13]))
>>> td_array
array([[ 5, 6, 7],
[ 8, 9, 10],
[11, 12, 13]])
>>>

In the preceding example, we created a 2D array named td_array. After creating an array, we printed td_array. Now we are also going to fetch the values in td_array through indexing. Let's look at an example to access values through indexing:

>>> td_array[1]
array([ 8, 9, 10])
>>>

In the preceding example, we accessed the first index value of the array and we got the output. In such a type of indexing, when we access the value, we get the whole array. Instead of getting the whole array, we can also get access to particular value. Let's look at an example:

>>> td_array[1,0]
8
>>>

In the preceding example, we accessed td_array by passing two values for the row and column. As seen in the output, we got the value 8.

We can also set up the two-dimensional array in a different way. First, set our 2D array with increased length. Let's set the length to 10. So, for that, we create a sample array with all zeros in it and, after that, we are going to put values in it. Let's look at an example:

>>> td_array = np.zeros((10,10))
>>> td_array
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> for i in range(10):
... td_array[i] = i
...
>>> td_array
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],
[6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],
[8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],
[9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])
>>>

In the preceding example, we created one two-dimensional array with the length 10 by 10.

Now let's do some fancy indexing on it, as shown in the following example:

>>> td_array[[1,3,5,7]]
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
[5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],
[7., 7., 7., 7., 7., 7., 7., 7., 7., 7.]])
>>>

In the preceding example, we fetch particular index values. So, in the result, we got the output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset