{"id":3486,"date":"2023-10-22T17:18:41","date_gmt":"2023-10-22T11:48:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=3486"},"modified":"2023-11-10T11:54:12","modified_gmt":"2023-11-10T06:24:12","slug":"python-numpy-select-rows-columns-by-index-from-a-2d-numpy-array-multi-dimension","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-select-rows-columns-by-index-from-a-2d-numpy-array-multi-dimension\/","title":{"rendered":"Python Numpy : Select rows \/ columns by index from a 2D Numpy Array | Multi Dimension"},"content":{"rendered":"
You have to be familiar with indexing if you want to work with Numpy arrays and matrices.\u00a0You will use them when you would like to work with a subset of the array.<\/p>\n
These dimentionals arrays are also known as matrices which can be shown as collection of rows and columns. In this article, we are going to show\u00a0 2D array in Numpy in Python. NumPy is a very important library in python. We used NumPy for adding support for large multidimensional arrays & matrices .<\/p>\n
Importing numpy module in your project:<\/p>\n
import<\/span> numpy <\/span>as<\/span> np<\/span>\r\n<\/pre>\nIndexing a Two-dimensional Array<\/h3>\nimport numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nprint(array1)\r\n<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[[ 0 1 2]\r\n[ 3 4 5]\r\n[ 6 7 8]\r\n[ 9 10 11]]\r\n<\/pre>\nHere we use two indices,one we use for rows and one for column.Both the column and the row indices start from 0.<\/p>\n
So in above all example we are going to use same matrices.<\/p>\n
Select a single element from 2D Numpy Array by index<\/h3>\n
We can use [][] tese brackets to select an element from Numpy Array .<\/p>\n
array1<\/span>[<\/span>row_index<\/span>][<\/span>column_index<\/span>]<\/span>\r\n<\/pre>\nSo if i want to access the element \u201810,\u2019 we use the index \u20183\u2019 for the row and index \u20181\u2019 for the column.<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\narray1= array1[3][1]\r\nprint(array1)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n\r\n10\r\n<\/pre>\nSelect Rows by Index from a 2D Numpy Array<\/h3>\n
We can use [] this bracket to select a single or multiple row. For single row use-<\/p>\n
array1<\/span>[<\/span>row_index<\/span>]<\/span>\r\n<\/pre>\nLet’s take an example,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nrow = array1[1]\r\nprint(row)\r\n<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[3 4 5]\r\n<\/pre>\nFor multiple rows use,<\/p>\n
array1<\/span>[<\/span>start_index: end_index , :<\/span>]<\/span>\r\n<\/pre>\nLet’s take an example,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nrows = array1[1:3, :]\r\nprint(rows)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[[3 4 5]\r\n[6 7 8]]\r\n<\/pre>\nSelect Columns by Index from a 2D Numpy Array<\/h3>\n
Syntax for single column use,<\/p>\n
array1<\/span>[<\/span> : , column_index<\/span>]<\/span>\r\n<\/pre>\nLets take an example,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\ncolumn = array1[:, 1]\r\nprint(column)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[ 1 4 7 10]\r\n<\/pre>\nSyntax to select multiple columns,<\/p>\n
ndArray<\/span>[<\/span> : , start_index: end_index<\/span>]<\/span>\r\n<\/pre>\nLets take an example,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\ncolumns = array1[: , 1:3]\r\nprint(columns)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[[ 1 2]\r\n[ 4 5]\r\n[ 7 8]\r\n[10 11]]\r\n<\/pre>\nSelect a Sub Matrix or 2d Numpy Array from another 2D Numpy Array<\/h3>\n
For sub 2d Numpy Array we pass the row & column index range in [] .<\/p>\n
Syntax for this is,<\/p>\n
ndArray<\/span>[<\/span>start_row_index : end_row_index , start_column_index : end_column_index<\/span>]<\/span>\r\n<\/pre>\nlets take an example,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nsub2DArr = array1[1:3, 1:3]\r\nprint(sub2DArr)\r\n<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[[4 5]\r\n[7 8]]\r\n\r\n<\/pre>\nSelected Row or Column or Sub Array is View only<\/h3>\n
This is for view only i.e. if there is\u00a0 any modification in returned sub array will be reflected in original Numpy Array .<\/p>\n
We are going to take same array which we have created in start of this article.<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nrow = array1[1]\r\nprint(row)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[3 4 5]\r\n<\/pre>\nNow for any modification in the same row,<\/p>\n
import numpy as np\r\narray1 = np.arange(12).reshape(4,3)\r\nrow = array1[1]\r\nrow[:] = 20\r\nprint(row)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[20 20 20]\r\n<\/pre>\nModification in sub array will be shown in main Numpy Array too. Updated \\ 2D Numpy Array array1<\/strong>\u00a0are,<\/p>\n[[ 0\u00a0 \u00a0 \u00a01\u00a0 \u00a0 \u00a0 2]\r\n[20 20 20]\r\n[ 6\u00a0 \u00a0 \u00a07\u00a0 \u00a0 \u00a0 8]\r\n[ 9\u00a0 \u00a0 10\u00a0 \u00a011]]\r\n<\/pre>\nGet a copy of 2D Sub Array from 2D Numpy Array using ndarray.copy()<\/h3>\n
Create a 2D Numpy array1 with3 rows & columns | Matrix<\/p>\n
import numpy as np\r\narray1 = np.array(([12, 22, 13], [21, 25, 30], [33, 17, 19]))\r\nprint(array1)<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[[12 22 13]\r\n[21 25 30]\r\n[33 17 19]]\r\n<\/pre>\nSelect a copy of row at index 1 from 2D array and set all the elements in selected sub array to 20.<\/p>\n
array1 = np.array(([12, 22, 13], [21, 25, 30], [33, 17, 19]))\r\nrow=array1[1].copy()\r\nrow[:] = 20\r\nprint(row)\r\n<\/pre>\nOutput:<\/strong><\/p>\nRESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n[20 20 20]\r\n<\/pre>\nConclusion:<\/h3>\n
So in this article we have seen how to work with NumPy arrays.Hope you have understood everything.<\/p>\n","protected":false},"excerpt":{"rendered":"
Working with Numpy Arrays: Indexing You have to be familiar with indexing if you want to work with Numpy arrays and matrices.\u00a0You will use them when you would like to work with a subset of the array. About 2d numpy array: These dimentionals arrays are also known as matrices which can be shown as collection …<\/p>\n