Updated April 3, 2023

## Introduction to NumPy 2D array

Python provides different functions to the users. To work with arrays, the python library provides a numpy function. Basically, 2D array means the array with 2 axes, and the array’s length can be varied. Arrays play a major role in data science, where speed matters. Numpy is an acronym for numerical python. Basically, numpy is an open-source project. Numpy performs logical and mathematical operations of arrays. In python, numpy is faster than the list. This is because numpy arrays can be stored in continuous places. Therefore, processing and manipulating can be done efficiently. In this topic, we are going to learn about NumPy 2D array.

**Syntax**

```
import numpy as np
a=np.array[[],[]]
```

**Explanation:**

In the above syntax, we first import a numpy class and access a numpy class as np, where a variable is variable, and we have created a 2-dimensional array. Here, we must assign row and column values to the array.

### How 2D arrays work in NumPy?

- We must install Python on your system.
- We must install numpy using the pip command.
- We required basic knowledge about Python.
- We required basic knowledge about 2D arrays.
- We can perform different operations on numpy 2D arrays.

Let’s see how we can implement numpy 2D arrays.

#### Example #1 – For 2 by 3 2D Array

**Code:**

```
import numpy as anp
A_x = anp.array([[1, 2, 4], [6, 9, 12]], anp.int32) #input array
print(type(A_x))
print("Shape of 2D Array: \n" ,A_x.shape)
print("Data type of 2D Array:", A_x.dtype)
print("2D Array:\n",A_x)
```

**Explanation:**

- We import numpy functions and use them as anp.
- We declared a variable for an input array such as A_x with array values.
- We tried to print the value of the input array with their Class, Shape, and Data type by using type, shape and dtype functions, respectively.
- Finally, we try to print a 2D array with dimensions 2 by 3.

In the above example, we implemented a 2D array. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #2 – Reshaping of 2D Array, 1D to 2D Array

**Code:**

```
import numpy as anp
Array = anp.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22])
new_array = Array.reshape(3, 4)
print("Reshape Array is :\n",new_array)
```

**Explanation:**

- We import numpy functions and use them as anp.
- We declared a variable for an input array such as Array with array values.
- Then, we used a reshape function to reshape the given array providing the values of row and column.
- Finally, we have printed the newly formed array.

In the above example, we implemented a 2D array with the reshape function. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #3 – Indexing 2D Array

**Code:**

```
import numpy as anp
Array = anp.arange(25).reshape(5,5)
print("Given array is \n",Array)
print("Output of indexing through row is \n",Array[3])
print("Output of indexing through column is \n",Array[:,4])
```

**Explanation:**

- We import numpy functions and use them as anp.
- We created an array consisting of 25 elements and then reshaped it as 5 rows and 5 columns using the reshape function.
- Then, we printed the array.
- Then, we have indexed the 2D array through the rows. Here, indexing is started from the 0th index; therefore, it will print the 4th row as shown in the snapshot.
- After that, we have indexed the 2D array through the column. Here, the column index is 4; therefore, it will return the 5
^{th}column as indexing starts from zero.

In the above example, we implemented a 2D array indexing through rows and columns. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #4 – slicing of 2D Array

**Code:**

```
import numpy as anp
Array = anp.arange(25).reshape(5,5) #input array
print("Given array is \n",Array)
print("Output is \n",Array[0:2,4]) #print slicing
```

**Explanation:**

- We import numpy functions and use them as anp.
- We created an array consisting of 25 elements and then reshaped it as 5 rows and 5 columns using the reshape function.
- Then, we try to print the array.

In the above example, we implemented slicing 2D arrays. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #5 – Slicing of 2D Array

**Code:**

```
import numpy as anp
Array = anp.arange(25).reshape(5,5)
print("Given array is \n",Array)
print("Slicing of array is \n",Array[2:5,0:3])
```

**Explanation:**

- We import numpy functions and use them as anp.
- We created an array consisting of 25 elements and then reshaped it as 5 rows and 5 columns using the reshape function.
- The given example of the indexing of rows started from row index 2 to index 4 as given in the condition. Also, indexing of columns started from column index 0 to index 2 as given in condition.
- Then, we try to print the array.

In the above example, we implemented a 2D array slicing for 2:5, 0:3. That means the user needs to print a particular section of the array at that time; we use the above condition. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #6 – Slicing of 2D Array

**Code:**

```
import numpy as anp
Array = anp.arange(25).reshape(5,5)
print("Given array is \n",Array)
print("Slicing of Array is \n",Array[3:4,3:4])
print("Slicing of Array is \n",Array[0::3])
```

**Explanation::**

- We import numpy functions and use them as anp.
- The given example of the indexing of rows started from row index 3 to index 4 as given in the condition. Also, the indexing of columns started from column index 3 to index 4 as given in condition.
- We can also print the interval of the array. Then, we try to print the array.

In the above example, we implemented a 2D array slicing for 3:4,3:4. That means the user needs to print a specific value of the array at that time we use the above condition. Illustrate the end result of the above declaration by using the use of the following snapshot.

### Conclusion

We hope from this article you have understood about the numpy 2D array. From the above article, we have learned basic syntax numpy 2D arrays. We have also learned how we can implement them in Python with different examples of each type. Finally, from this article, we have learned how we can handle numpy 2D in python.

### Recommended Articles

This is a guide to NumPy 2D array. Here we discuss How 2D arrays work in NumPy and how we can implement them in Python with different examples of each type. You may also have a look at the following articles to learn more –