How to Retrieve an Entire Row or Column of an Array in Python?
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:
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:
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 :
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.
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]]