The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements.
Selecting a single element from a NumPy array
Each element of these ndarrays can be accessed using its index number.
Example: The following code shows how to access an element of a NumPy array.
numpyArr = 1 numpyArr[-1] = 4
In the first case, we accessed the first element of the array using its index number. In the second case we accessed the last element of the array by using negative indexes.
Selecting a sub array from a NumPy array (Slicing)
To get a sub-array, we pass a slice in place of the element index.
Here x and y are the starting and last index of the required sub-array.
Sub-Array= [4 5 6]
A sub-array starting from the 3rd index up to the 6th index ( excluding the last the 6th index ) was selected. You can slice a sub-array starting from the first element by leaving the starting index blank.
Example: The following code selects a sub-array starting from the first element.
Sub-Array= [1 2 3 4 5 6]
Similarly, leaving the left side of the colon blank will give you an array up to the last element.
Example: The following code selects a sub-array starting from a particular index up to the last index.
Sub-Array= [4 5 6 7 8 9]
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Select a row of series or dataframe by given integer index
- numpy.select() function | Python
- Dunn index and DB index - Cluster Validity indices | Set 1
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- How to rearrange columns of a 2D NumPy array using given index positions?
- numpy.index() in Python
- numpy string operations | index() function
- Python | Numpy numpy.resize()
- Python | Numpy numpy.transpose()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__ge__()
- Python | Numpy numpy.ndarray.__eq__()
- Python | Numpy numpy.ndarray.__ne__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.