# Select Rows / Columns by Index in NumPy Array

In this article, we explore how to select elements, rows, columns, and sub-arrays from a 2D NumPy array, which is an essential skill in data analysis and manipulation.

Importing NumPy Module

First, we import NumPy, which is a fundamental package for scientific computing in Python:

```import numpy as np
```

NumPy provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.

Creating a 2D NumPy Array

We create a 2D array (matrix) using `np.array()` by passing a list of lists:

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

print(nArr2D)
```

Output:

```[[21 22 23]
[11 22 33]
[43 77 89]]
```

Each inner list becomes a row in the 2D array. The above code creates a 3×3 matrix.

## Select an Element from 2D NumPy Array

We can select a single element from the array using its row and column indices:

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

num = nArr2D
print('Element at row index 1 & column index 2 is:', num)
```

Output:

```Element at row index 1 & column index 2 is: 33
```

This code snippet accesses the second row (`index 1` as indexing starts at 0) and the third column (`index 2`). The output is `33`, the element at that position.

Alternatively, we can use a tuple of indices:

```num = nArr2D[1, 2]
print('Element at row index 1 & column index 2 is:', num)
```

Output:

```Element at row index 1 & column index 2 is: 33
```

This is a more concise way to achieve the same result.

## Select Rows from 2D NumPy Array

• To select an entire row from a 2D Array, we can pass the row index in [] operator. Like `arr[row_index]`. It will return a complete row at given index.
• To select multiple rows use: `arr[start_index: end_index , :]`. It will return rows from `start_index` to `end_index – 1` and will include all columns.

Let’s see an example, To select entire 2nd row from a 2D Array, we can pass the row index `1` in [] operator. Like this,

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

# Select row at index 1 i.e. 2nd row
row = nArr2D

print('Row at Index 1:', row)
```

Output:

```Row at Index 1: [11 22 33]
```

This snippet selects the second row of the array. Remember, in Python, indexing starts at 0.

For selecting multiple rows:

```rows = nArr2D[1:3, :]

print('Rows from Index 1 to 2:\n', rows)
```

Output:

```Rows from Index 1 to 2:
[[11 22 33]
[43 77 89]]
```

Here, `1:3` is a slice indicating the second and third rows (excluding row at index 3).

## Select Columns from 2D NumPy Array

To select a column, pass the column index along with the rows information in the [] operator of NumPy Array.

```arr[ : , column_index]
```

It will return a complete column at given index. To select multiple columns use,

```ndArray[ : , start_index: end_index]
```

It will return columns from start_index to end_index – 1.

Let’s see an example,

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

# Select 2nd Column from 2D Array
column = nArr2D[:, 1]

print('Column at Index 1:', column)
```

Output:

```Column at Index 1: [22 22 77]
```

The `:` means “select all rows,” and `1` specifies the second column.

For multiple columns:

```columns = nArr2D[:, 1:3]

print('Columns from Index 1 to 2:\n', columns)
```

Output:

``` [[22 23]
[22 33]
[77 89]]
```

This selects columns 2 and 3 (`1:3` slice).

## Select a Sub Matrix from a NumPy Array

To select sub 2d Numpy Array we can pass the row & column index range in [] operator i.e.

```arr[start_row_index : end_row_index , start_column_index : end_column_index]
```

It will return a sub 2D Numpy Array for given row and column range.

Let’s use these, to select a sub-matrix from a 2D NumPy Array,

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

# Select sub-Array i.e. from rows 1 to 2 and columns 1 to 2
sub2DArr = nArr2D[1:3, 1:3]

print('Sub 2D Array:\n', sub2DArr)
```

Output:

```Sub 2D Array:
[[22 33]
[77 89]]
```

This snippet selects rows 2 to 3 and columns 2 to 3, forming a smaller 2×2 matrix.

## Selecting a View vs Selecting a Copy from a NumPy Array

Modifying a view will affect the original array:

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

nArr2D = [100, 100, 100]

print('Modified Array:\n', nArr2D)
print('Original Array:\n', nArr2D)
```

Output:

```Modified Array:
[[ 21  22  23]
[100 100 100]
[ 43  77  89]]
Original Array:
[[ 21  22  23]
[100 100 100]
[ 43  77  89]]
```

Changing the second row in the view alters the original array.

To avoid this, create a copy:

```import numpy as np

nArr2D = np.array([[21, 22, 23],
[11, 22, 33],
[43, 77, 89]])

row_copy = nArr2D.copy()
row_copy[:] = 200

print('Modified Array:\n', row_copy)
print('Original Array:\n', nArr2D)
```

Output:

```Modified Array:
[200 200 200]
Original Array:
[[21 22 23]
[11 22 33]
[43 77 89]]
```

Modifying `row_copy` doesn’t change `nArr2D` because `row_copy` is an independent copy.

## Summary

Today, we learned how to select rows & columns from a 2D NumPy Array in Python.

Scroll to Top