In this post, We will learn, How to select row by Index in the NumPy array. To select the element from NumPy or Matrix by Index, we pass the rows and columns index inside the square bracket [][]. We will cover select elements by row and column index in 2D Numpy array.
1. How to select single element by Index of NumPy array
To select a single element from the NumPy array by index. We will pass the index of rows and columns in the indexing bracket [][].In the below example we are selecting an element that is row index is 1 and column index is 3.
Syntax
#Select a Single element of NumPy array by Index
nparray[rowindex][colIndex]
nparray[rowIndex,colIndex]
- By rows and column index : npArr[1][3]
- by rows and column index as a list : npArr[1,3]
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
#one way select element from array or matrix
print('Way 1:select Single element from array by index: \n',npArr[1][3])
#another way to select
print('Way2 : Single element from array by index: \n',npArr[1,3])
Output
Way 1:select Single element from array by index:
24
Way2 : Single element from array by index:
24
- How to select last N rows of NumPy array
- How to get first n rows of NumPy array
- How-to-select-rows-from-numpy-array
2. Select single row of NumPy array by Index
We will learn How to select row by Index in NumPy array 2D.To select a single row by index the below syntax is used.
Syntax
#Select a Single row of NumPy array by index
nparray[rowindex]
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Single row of NumPy by Index: \n',npArr[1])
Output
Single row of NumPy by Index :
[15 18 21 24]
3. How to Select Multiple rows by Index
To select the multiple row or range of rows from numpy 2D array or matrix. We have to pass the range of row Index inside square bracket . In this example we are selecting rows that start from index 1 and end at index 4..The syntax for this is
Syntax
#Select multiple rows of NumPy array or matrix by Index
nparray[startrowIndex : endrowIndex , :]
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Access rows of 2D array: \n',npArr[1:4, :])
Output
Access rows of 2D array:
[[15 18 21 24]
[ 7 14 21 28]
[35 42 49 56]]
4. Select all rows by Index NumPy array
In this example we are selecting all rows of numpy 2D array that starts from row index 0 to Endindex.If we would not specify the startIndex and endIndex row indices instead of this we pass double colon :: for row start and end index it will select all rows from start to end as in below example
- By using double colon for startIndex and endIndex row index as in below example npArr[::, :]
- The another way to achieve this by pass row EndIndex and skip start Index as in below example,We have passed npArr[:5, :]
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Access rows of 2D array: \n',npArr[:5, :])
#another way to all rows from start to end
print('NumPy array Mutiple rows from start to End : \n',npArr[::, :])
Output
Access rows of 2D array:
[[ 3 6 9 12]
[15 18 21 24]
[ 7 14 21 28]
[35 42 49 56]]
NumPy array Mutiple rows from start to End
[[ 3 6 9 12]
[15 18 21 24]
[ 7 14 21 28]
[35 42 49 56]]
Syntax to Select Column by Index
- Select Single column from NumPy array:
- nparray[:, colIndex]
- Select Multiple columns by Index :
- nparray[ : ,colStartIndex:colEndIndex]
- Select Mutiple rows and columns by Indices :
- nparray[startrowIndex : endrowIndex , startcolIndex : endcolIndex]
5. How to Select single column by index
Sometimes we have to select a single column but all its rows by Index . To select a column by index we have to pass the index of column by using index bracket [] using this syntax npArr[:,colIndex] .The colIndex can be 1,2,3.. N as per numpy array.In this below example we are accessing the single first column by index.
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Selected single column by Index: : \n',npArr[:,1])
Output
Selected single column by Index:
[ 6 18 14 42]
6. How to select Range or Mutiple coulmns by Index
In this example we are selecting the range of column by index.To select mutiple range of column or mutiple columns by index we have to pass Column start index and column end index by using this syntax nparray[ : ,colStartIndex:colEndIndex] as in below example [:,1:3] the start column index is 1 and end column index is 3.
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Selected Mutiple column by Index: \n',npArr[:,1:3])
Output
Selected row by index start to End:
[[ 6 9]
[18 21]
[14 21]
[42 49]]
7. Select Mutiple rows and colmns by Index
In this example, we will learn, how to select multiple rows and columns by index.Tp select Mutiple rows and columns by Indices :
nparray[startrowIndex : endrowIndex , startcolIndex : endcolIndex] in below example nparr[1:4,1:4] mean select row start from index 1 and endindex is 4 and same for column.
import numpy as np
npArr = np.array([[3,6,9,12],[15,18,21,24],[7,14,21,28],[35,42,49,56]])
print('Selected Mutiple rows and columns by Index : \n',npArr[1:4,1:4])
Output
Selected Mutiple rows and columns by Index
[[18 21 24]
[14 21 28]
[42 49 56]]
Summary
In this post, we have learned how to select row by Index NumPy array 2D with examples.