Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python Numpy : Select rows / columns by index from a 2D ndarray | Multi Dimensio ...

$
0
0

In this article we will discuss how to select elements from a 2D ndarray. Elements to select can be a an element only or single/multiple rows & columns or an another sub 2D array.

First of all, let’s import numpy module i.e.

import numpy as np

Now let’s create a 2d ndArray by passing a list of lists to numpy.array() i.e.

# Create a 2D Numpy adArray with 3 rows & 3 columns | Matrix nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

Contents of the 2D ndArray will be,

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

Now let’s see how to select elements from this 2D ndarray by index i.e.

Select a single element from 2D ndarray by index We can use [][] operator to select an element from ndarray i.e. ndArray[row_index][column_index] Example 1:

Select the element at row index 1 and column index 2.

# Select element at row index 1 & column index 2 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 Example 2:

Or we can pass the comma separated list of indices representing row index & column index too i.e.

# Another way to select element at row index 1 & column index 2 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 Select Rows by Index from a 2D ndarray We can call [] operator to select a single or multiple row.To select a single row use, ndArray[row_index]

It will return a complete row at given index.

To select multiple rows use,

ndArray[start_index: end_index , :]

It will return rows from start_index to end_index 1 and will include all columns.

Let’s use this,

Contents of the 2D a ndArray nArr2D created above are,

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

Let’s select a row at index 2 i.e.

# Select a Row at index 1 row = nArr2D[1] print('Contents of Row at Index 1 : ' , row)

Output:

Contents of Row at Index 1 :[11 22 33]

Select multiple rowsfrom index 1 to 2 i.e.

# Select multiple rows from index 1 to 2 rows = nArr2D[1:3, :] print('Rows from Index 1 to 2 :') print(rows)

Output:

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

Select multiple rowsfrom index 1 to last index

# Select multiple rows from index 1 to last index rows = nArr2D[1: , :] print('Rows from Index 1 to last row :') print(rows)

Output:

[[11 22 33] [43 77 89]] Select Columns by Index from a 2D ndArray

To select a single column use,

ndArray[ : , 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 use these,

Contents of the 2D ndArray nArr2D created above are,

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

Select a columnat index 1

# Select a column at index 1 column = nArr2D[:, 1] print('Contents of Column at Index 1 : ', column)

Output:

Contents of Column at Index 1 :[22 22 77]

Select multiple columnsfrom index 1 to 2

# Select multiple columns from index 1 to 2 columns = nArr2D[: , 1:3] print('Column from Index 1 to 2 :') print(columns)

Output:

Column from Index 1 to 2 : [[22 23] [22 33] [77 89]]

Select multiple columnsfrom index 1 to last index

# Select multiple columns from index 1 to last index columns = nArr2D[:, 1:]

Output is same as above because there are only 3 columns 0,1,2. So 1 to last columns means columns at index 1 & 2.

Select a Sub Matrix or 2d ndarray from another 2D ndarray To select sub 2d ndArray we can pass the row & column index range in [] operator i.e. ndArray[start_row_index : end_row_index , start_column_index : end_column_index]

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

Let’s use these,

Contents of the 2D ndArray nArr2D created at start of article are,

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

Select a sub 2D ndarray from row indices 1 to 2 & column indices 1 to 2

# Select a sub 2D array from row indices 1 to 2 & column indices 1 to 2 sub2DArr = nArr2D[1:3, 1:3] print('Sub 2d Array :') print(sub2DArr)

Output:

Sub 2d Array : [[22 33] [77 89]] Selected Row or Column or Sub Array is View only Contents of the ndarray selected using [] operator returns a View only i.e. any modification in returned sub array will be reflected in original ndarray.

Let’s check this,

Contents of the 2D ndArray nArr2D created at start are,

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

Select a row at index 1 from 2D array i.e.

# Select row at index 1 from 2D array row = nArr2D[1]

Contents of row :

[11 22 33]

Now modify the contents of row i.e.

# Change all the elements in selected sub array to 100 row[:] = 100

New contents of the row will be

[100 100 100]

Modification in sub array will be reflected in main ndArray too. Updated Contents of the 2D ndArray nArr2D are,

[[ 212223] [100 100 100] [ 437789]] Get a copy of 2D Sub Array from 2D ndArray using ndarrat.copy()

to the copy instead of view in sub array use copy() function.

Let’s check this,

Create a 2D Numpy adArray with3 rows & columns | Matrix

# Create a 2D Numpy adArray with3 rows & columns | Matrix nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

Content of nArr2D is,

[[ 212223] [100 100 100] [ 437789]]

Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 100

# Select a copy of row at index 1 from 2D array row = nArr2D[1].copy() # Set all the elements in selected sub array to 100 row[:] = 100

Here, sub array is a copy of original array so, modifying it will not affect the original ndArray

Contents of the modified sub array row is,

[100 100 100]

Contents of the original ndArray is,

[[21 22 23] [11 22 33] [43 77 89]] Complete example is as follows, import numpy as np def main(): # Create a 2D Numpy adArray with 3 rows & 3 columns | Matrix nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89])) print('Contents of 2D Array : ') print(nArr2D) print('*** Select an element by index from a 2D ndArray') # Select element at row index 1 & column index 2 num = nArr2D[1][2] print('element at row index 1 & column index 2 is : ' , num) # Another way to select element at row index 1 & column index 2 num = nArr2D[1, 2] print('element at row index 1 & column index 2 is : ', num) print('*** Select Rows by Index from a 2D ndArray ***') # Select a Row at index 1 row = nArr2D[1] print('Content

Viewing all articles
Browse latest Browse all 9596

Trending Articles