Skip to content
Related Articles

Related Articles

How to Retrieve an Entire Row or Column of an Array in Python?
  • Last Updated : 12 Nov, 2020
GeeksforGeeks - Summer Carnival Banner

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

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 package
import numpy as np
  
# creating a numpy character array
arr1 = np.array(["Ram", "Shyam" , "Sita"])
  
print("First row - ")
print(arr1)
  
# printing first column referred by first index
print ("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 package
import numpy as np
  
# creating a numpy integer array
arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  
print("First two columns")
print(arr1[0:2])
  
# printing columns in a range
print("Columns in a range")
print(arr1[4:7])
  
# computing length of array
length = 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 

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 package
import numpy as np
  
# creating a numpy integer array
mat1 = np.array([[1, 2, 3], [4, 5, 6]])
  
print("Original matrix ")
print(mat1)
  
print("Row at 0th index")
print(mat1[0])
  
# 1st columns
print("Column at 1st index")
print(mat1[:, 1])
  
# computing length of array
print("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 package
import numpy as np
  
# creating a numpy integer array
mat1 = 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 columns
print("Last three columns")
  
# prints all the columns till the end
print(mat1[:, 1:])
  
# printing a subset of matrix
print("Matrix subset")
# row index 1 and 2 inclusive and col_index 2 and 3 inclusive
print(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]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :