How to Retrieve an Entire Row or Column of an Array in Python?

12 Nov, 2020
Arrays are a set of similar elements grouped together to form a single entity, that is, it is basically a collection of integers, floating-point numbers, characters etc. The indexing of the rows and columns start from 0.
Uni-dimensional arrays form a vector of similar data-type belonging elements. It contains a single row of elements, each of them falling into different columns. The dimensions of the uni-dimensional array are [1 x c], where c is the number of columns. It is possible to access any column from the array using its corresponding index. Since, this array contains a single row, printing the array is equivalent to printing the first row. 
array - retrieves the column at cth index (c+1 row)

The following Python code illustrates the process of retrieving either an entire column in a 1-D array:
Python
# importing the required packageimport numpy as np  # creating a numpy character arrayarr1 = np.array(["Ram", "Shyam" , "Sita"])  print("First row - ")print(arr1)  # printing first column referred by first indexprint ("First Column")print (arr1[0])  # computing length of array length = len(arr1)print("Last Column")print (arr1[length-1])
Output:
Original Array -  
['Ram' 'Shyam' 'Sita']
First Column
Ram
Last Column
Sita

It is also possible to retrieve a range of columns from the uni-dimensional array by specifying the start and the last index. If we do not specify the last index, the array is printed till the end of the array.
array[start : end] – retrieves the array columns from start index to end-1 index.
The following python code is used to retrieve a range of columns in a 1-D array:
Python3
# importing the required packageimport numpy as np  # creating a numpy integer arrayarr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])  print("First two columns")print(arr1[0:2])  # printing columns in a rangeprint("Columns in a range")print(arr1[4:7])  # computing length of arraylength = len(arr1)  print("Last 3 Columns")print(arr1[length-3:length])  print("Array till the end")print(arr1[3:])
Output:
First two columns
[1 2]
Columns in a range
[5 6 7]
Last 3 Columns
[6 7 8]
Array till the end
[4 5 6 7 8]

Multi-Dimensional Array is a sequence of rows stacked together to form a matrix. The matrix contains similar elements, belonging to either integers, characters and double numbers. It is referred by the dimensions [r x c] , where r is the number of rows and c is the number of columns. 
matrix [r] - prints row at r index
matrix[ : , c] - prints column at c index

The following Python code illustrates the process of retrieving either an entire row or a column :
Python3
# importing the required packageimport numpy as np  # creating a numpy integer arraymat1 = np.array([[1, 2, 3], [4, 5, 6]])  print("Original matrix ")print(mat1)  print("Row at 0th index")print(mat1[0])  # 1st columnsprint("Column at 1st index")print(mat1[:, 1])  # computing length of arrayprint("Column at 2nd index")print(mat1[:, 2])
Output:
Original matrix 
[[1 2 3]
 [4 5 6]]
Row at 0th index
[1 2 3]
Column at 1st index
[2 5]
Column at 2nd index
[3 6]

It is also possible to prints rows or columns belonging to a range in the matrix. We specify the beginning and ending indexes of the rows and columns of the matrix. If we leave the end index blank, it prints the columns or rows till the length of the matrix. 
Python3
# importing the required packageimport numpy as np  # creating a numpy integer arraymat1 = np.array([[1, 2, 3, 4], [4, 5, 6, 8], [7, 6, 8, 9]])print("Original matrix ")print(mat1)  print("Row from 1st to 2nd index")print(mat1[1:3])  # 1st columnsprint("Last three columns")  # prints all the columns till the endprint(mat1[:, 1:])  # printing a subset of matrixprint("Matrix subset")# row index 1 and 2 inclusive and col_index 2 and 3 inclusiveprint(mat1[1:3, 2:4])
Output: 
Original matrix 
[[1 2 3 4]
 [4 5 6 8]
 [7 6 8 9]]
Row from 1st to 2nd index
[[4 5 6 8]
 [7 6 8 9]]
Last three columns
[[2 3 4]
 [5 6 8]
 [6 8 9]]
Matrix subset
[[6 8]
 [8 9]]

Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.