- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How To Access Different Rows Of A Multidimensional Numpy Array?

## NumPy Multidimensional Arrays

As the name suggests, Multidimensional Arrays are a technique that can be described as a way of defining and storing data in a format that has more than two dimensions (2D). Python allows the implementation of Multidimensional Arrays by nesting a list function inside another list function.

Here are some examples on how we can create single and multidimensional arrays in Python using Numpy.

### Single Dimensional Array

#### Example

import numpy as np simple_arr = np.array([0, 1, 2, 3, 4]) print(simple_arr )

#### Output

[0 1 2 3 4]

### Algorithm

Import the NumPy library

Use the NumPy array() function to create a one-dimensional array

Use the print() function to print the array's contents

### Two Dimensional Array

#### Example

import numpy as np simple_arr = np.array([[1, 2, 3], [4, 5, 6]]) print(simple_arr )

#### Output

[[1 2 3] [4 5 6]]

### Algorithm

Import the NumPy library

Use the NumPy array() function to create a Two dimensional array

Use the print() function to print the array's contents

### Three Dimensional Array

#### Example

import numpy as np simple_arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) print(simple_arr )

#### Output

[[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]]

### Algorithm

Import the NumPy library

Use the NumPy array() function to create a Three dimensional array

Use the print() function to print the array's contents

## Accessing 2D NumPy Array Rows

In Python, we can access the elements of a two-dimensional array using two indices to reach their respective positions. The first index refers to the position of the elements on the list, while the second index refers to the order in which they appear. When we define only one index with an array name, the index returns all the elements stored in that array.

### Indexing a Two-dimensional Array

#### Example

Consider a two-dimensional array −

import numpy as np array1 = np.arange(12).reshape(4,3) print(array1)

#### Output

array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])

### Algorithm

Make a one-dimensional array with the input 12 by using the np.arange() method.

Then, use the reshape() function to transform the one-dimensional array into a two-dimensional array

Assign the result to array1.

Use the print() method to print the contents of array1

We can access elements in this array via two indices, one for each row and one for each column. Both indices start at 0. So, for example, to access '8,' use the index '2' for the row and index '2' for the column.

print(array1[2][2])

#### Output

8

In Python, multidimensional indexing arrays work just like 2D or 3D arrays. Just use array[index1, index2, index3] to get a specific element in a multidimensional array. You just need to add indexN as per the dimension of the array.

### Slicing a 2D Array

Slicking is similar to indexing, but instead of selecting one specific element from the array, it selects a subset of elements using a range of indices along one or both dimensions.

If you want to slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index].

For slicing multiple elements, you can also use ranges for row indexes and column indexes −

[start_row_index:end_row_index, start_column_index:end_column_index]

#### Example

For example, you can select elements starting at [0:1, 0:2] across the first two columns of the first row.

import numpy as np array1 = np.arange(16).reshape(4,4) print(array1) print(array1[0:2, 1:3])

#### Output

[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[1 2] [5 6]]

### Algorithm

Using numpy's arange method, create a 4x4 numpy array and reshape it to a 4x4 matrix.

Print the entire 4x4 matrix from the array

Slice the original array to extract a sub-matrix −

Use the indexing 0:2 to specify the first two rows of the original array.

By using the indexing 1:3, specify the second and third columns of the array.

Slice the original array using these two index ranges.

Print the submatrix extracted through slicing.

If you want to slice a multidimensional array in NumPy, then you can use the same syntax as for slicing a 2D or 3D array;

array [start:end, start:end, start:end]

For example

my_array[1:3, 1:4, 1:3]

will allow you to slice a multidimensional array along any dimension.

## Conclusion

NumPy Multidimensional Arrays are a powerful tool for data analysis and manipulation. They allow us to store and access data in a structured manner, making it easier to work with large datasets.

This article discussed how to access different rows of a multidimensional NumPy array. We discussed using single and multi-dimensional arrays, indexing and slicing arrays, and accessing 2D NumPy Array rows. We also examined some examples of how these techniques can be used in practice.

With this knowledge, you should be able to effectively access different rows of a multidimensional NumPy array in your own projects.

- Related Articles
- How to access a group of rows in a Pandas DataFrame?
- How to convert Multidimensional PHP array to JavaScript array?
- How to print dimensions of multidimensional array in C++
- Mask rows of a 2D array that contain masked values in Numpy
- Initialization of a multidimensional array in C
- How to do multidimensional array intersection using JavaScript?
- How to sort a Numpy Array?
- PHP Multidimensional Array.
- How to check for multidimensional nature of an array in PHP
- Suppress whole rows of a 2-D array that contain masked values in Numpy
- How to set multidimensional array into JTable with Java?
- Multidimensional array in Java
- MongoDB multidimensional array projection?
- Mask rows and/or columns of a 2D array that contain masked values in Numpy
- How to split a column of list in different rows using Bootstrap?