How to print an array as a column in python

In this article we will discuss how to select elements from a 2D Numpy Array . 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 Numpy Array 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 Numpy Array will be,

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

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

Select a single element from 2D Numpy Array by index

We can use [][] operator to select an element from Numpy Array 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:

Advertisements

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

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 Numpy Array 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 rows from 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 rows from 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 Numpy Array

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 Numpy Array nArr2D created above are,

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

Select a column at 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 columns from 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 columns from 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 Numpy Array from another 2D Numpy Array

To select sub 2d Numpy Array 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 Numpy Array for given row and column range.

Let’s use these,

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

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

Select a sub 2D Numpy Array 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 Numpy Array selected using [] operator returns a View only i.e. any modification in returned sub array will be reflected in original Numpy Array .
Let’s check this,

Contents of the 2D Numpy Array 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 Numpy Array too. Updated Contents of the 2D Numpy Array nArr2D are,

[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]

Get a copy of 2D Sub Array from 2D Numpy Array using ndarray.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,

[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]

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 Numpy Array
Contents of the modified sub array row is,

[100 100 100]

Contents of the original Numpy Array 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['Contents of Row at Index 1 : ' , row]

   # Select multiple rows from index 1 to 2
   rows = nArr2D[1:3, :]

   print['Rows from Index 1 to 2 :']
   print[rows]

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

   print['*** Select Columns by Index from a 2D ndArray ***']

   # Select a column at index 1
   column = nArr2D[:, 1]

   print['Contents of Column at Index 1 : ', column]

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

   print['Column from Index 1 to 2 :']
   print[columns]

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

   print['Column from Index 1 to last index :']
   print[columns]

   print['*** Select a Sub Matrix or 2d Array from another 2D ndArray ***']

   print['Original ndArray']
   print[nArr2D]

   # 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]



   print['*** Sub Array is View only ***']

   print['Original ndArray']
   print[nArr2D]

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

   print['Contents of row / sub array']
   print[row]

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

   # As sub array is a copy so, changes in it will be reflected in original array too

   print['Contents of modified row / sub array']
   print[row]
   print['Original ndArray']
   print[nArr2D]

   print['*** Fetching a copy of 2D Sub Array from 2D ndArray ***']

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

   # 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
   '''

   print['Contents of modified row / sub array']
   print[row]
   print['Original ndArray']
   print[nArr2D]



if __name__ == '__main__':
   main[]

Output:

Contents of 2D Array : 
[[21 22 23]
 [11 22 33]
 [43 77 89]]
*** Select an element by index from a 2D ndArray
element at row index 1 & column index 2 is :  33
element at row index 1 & column index 2 is :  33
*** Select Rows by Index from a 2D ndArray ***
Contents of Row at Index 1 :  [11 22 33]
Rows from Index 1 to 2 :
[[11 22 33]
 [43 77 89]]
Rows from Index 1 to last row :
[[11 22 33]
 [43 77 89]]
*** Select Columns by Index from a 2D ndArray ***
Contents of Column at Index 1 :  [22 22 77]
Column from Index 1 to 2 :
[[22 23]
 [22 33]
 [77 89]]
Column from Index 1 to last index :
[[22 23]
 [22 33]
 [77 89]]
*** Select a Sub Matrix or 2d Array from another 2D ndArray ***
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]
Sub 2d Array :
[[22 33]
 [77 89]]
*** Sub Array is View only ***
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]
Contents of row / sub array
[11 22 33]
Contents of modified row / sub array
[100 100 100]
Original ndArray
[[ 21  22  23]
 [100 100 100]
 [ 43  77  89]]
*** Fetching a copy of 2D Sub Array from 2D ndArray ***
Contents of modified row / sub array
[100 100 100]
Original ndArray
[[21 22 23]
 [11 22 33]
 [43 77 89]]

 

How do you print an array in a column in Python?

Directly printing using the print[] method We can directly pass the name of the array[list] containing the values to be printed to the print[] method in Python to print the same. But in this case, the array is printed in the form of a list i.e. with brackets and values separated by commas.

How do I print an array in a column?

loop through the array and gather each nth value in to another array, that has row and column reversed, and print it out. This is called “transposing”..
STEP 1: START..
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}..
STEP 3: length= sizeof[arr]/sizeof[arr[0]].
STEP 4: PRINT "Elements of given array:".
STEP 5: i=0. REP..

How do you print an array in Python?

PROGRAM:.
#Initialize array..
arr = [1, 2, 3, 4, 5];.
print["Elements of given array: "];.
#Loop through the array by incrementing the value of i..
for i in range[0, len[arr]]:.
print[arr[i]],.

How do you print columns in Python?

Print With Column Alignment in Python.
Use the % Formatting to Print With Column Alignment in Python..
Use the format[] Function to Print With Column Alignment in Python..
Use f-strings to Print With Column Alignment in Python..
Use the expandtabs[] Function to Print With Column Alignment in Python..

Bài mới nhất

Chủ Đề