Numpy Create 2D array
In simple words, we can define the two-dimensional array as an array within another array. Its index starts with “0” and ends at the size of array “-1”. Arrays can be constructed n times within arrays. A two-dimensional array can change size both vertically and horizontally, in both directions.
The syntax to declare an array is as below:
array_name is the name of the array that we want to create. Whereas, “r_arr” are the rows of the array and “c_arr” is the column of the array. This syntax allows us to create the memory location where the array will be stored, or we can say that the memory location can be reserved for the array.
There is another method to declare a 2D array:
In the syntax above, array name is the name of the array where “R1C1”, “R2C1”, … n are the elements of the array where “R” denotes rows and “c” denotes columns. As we can see in the first square braces, the number of rows is changing while the columns are the same. This is because, within the array, we define columns using multiple arrays while the rows are defined inside the inner arrays.
Example # 01: Creating a Two-dimensional Array
Let us give a practical example of creating a two-dimensional array and get a better idea of how a two-dimensional array is created. To create a 2D array, we will first import our NumPy library which will enable us to implement some packages that NumPy provides us with for creating the array. Next, we will initialize a variable that holds the two-dimensional array to create an array. We will pass the np.array() function that allows us two create any type of array whether it is 1D, 2D, or so on. To that function, we will pass multiple arrays within this array which lets us create a 2-dimensional array.
As we can see in the screenshot below, in the second line, we passed three arrays to that function which means we have three rows and within those arrays, we passed 6 elements to each which means there are 6 columns. One thing to notice, is we always pass elements in square brackets which means we are passing array elements and we can see that we have passed multiple arrays within the single array.
In the end, we printed the array using a print statement. As shown in the screenshot below, we can see the array is displayed which contains 3 rows and 6 columns.
Example # 02: Accessing the Values
As we studied the method to create 2D arrays, one thing must have clicked in our mind: how can we access the elements of the 2D array? While accessing the element of the 2D array is not a big issue. Numpy enables us to manipulate the elements of arrays by a simple line of code that is:
The array is the name of the array from which we have to access or fetch the data where the row index is the memory location of the row. And the column index is the location of the column to be accessed, suppose we have to access the index “2” element of row and index “0” element of a column.
As we can see in the figure below, we first imported the NumPy library to access the packages of NumPy. Then, we declared the variable name “array” which holds the 2D array and then passed it the values which we want to store in it. We first displayed the array as it is which we have initialized. Then, we passed the array with the index to our print() statement which will display the whole array that is stored at index “2”. In the next line of code, we again passed the array with two indexes to the print() statement. The first one is the row of the array and the second one is the column of the array which is “0” and “2”.
The following output is returned while running the code compiler prints the array as it is. Then, the second row according to the code. Lastly, the compiler returns the element that is stored at index “0” for rows and index “2” for the column.
Example #03: Updating the Values
We have already discussed the methodology of how we can create or access the data or elements within the 2D array, but when we have to change the elements of the array, we can simply use the method that is provided by the NumPy packages that allow us to update the desired value within an array.
To update the value, we use:
In the syntax above, the array is the name of the array. The row index is the place or location which we will edit. The column index is the location of the column at which the value is updated, where the value is the one that should be added to the desired index.
As we can see, we first import our NumPy library. And then declared an array of size 3×6 and passed its integer values. Then, we passed the value “21” to the array which means we want to store the value “21” in the array at “0” of a row and “2” of a column which means we want to store it at the index of the first row and the 3rd column of the array. Then print both arrays, the original one and also the element that we have stored in the array.
As displayed below, the value is successfully updated in the array by just adding a simple line of code that is provided by the NumPy package.
In this article, we explained different ways to create two-dimensional arrays and how we can manipulate them using NumPy’s built-in functions. We discussed how we can access the elements within the array and update them. Numpy enables us to create and manipulate multi-dimensional arrays by a single line of code. Numpy arrays are clearer and more effective than python lists.