Fancy Indexing
Let’s learn about fancy indexing in Python.
Fancy indexing allows us to select entire rows or columns out of order.
Let’s create a NumPy array to see how it works!
Do you remember
zeros()
,range()
,shape
andbroadcasting
?
Let’s revise these concepts:
- Setting
array_2d = np.zeros((5,5))
creates a 2-D array of 5 rows and 5 columns containing all zeros. - Setting
array_2d[1]=1
broadcasts 1 to the complete 2nd row. - Setting
array_2d[2]=2
broadcasts 2 to the 3rd row. - Setting
array_2d[3]=3
broadcasts 3 to the 4th row. - Setting
array_2d[4]=4
broadcasts 4 to the 5th row. - Passing
print(array_2d)
prints a matrix.
Let’s also try using the for
loop for the tasks given above to see if it works. The comments are provided for revision:
Get hands-on with 1200+ tech skills courses.