Access a NumPy array by column in Python

In this tutorial, We will learn how to access a NumPy array by column in Python. First of all, let us see what an array is. An array is a particular data type used to store items or values contiguously. Arrays are the same as the list in python but the only main difference is that the array can only store the same type of elements whereas lists can store different kinds of elements.

Creating an array using NumPy

There are multiple ways to create an array one is by using the array() is a function in NumPy which is used to create an array in Python for that firstly the package should be imported and the method can be accessed let us see it with an example

# Creating an array
import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print("The created array is\n"a)

Output:

The created array is
[[1 2 3]
[4 5 6]
[7 8 9]]

This is an example of array creation in Python using the NumPy package and there are multiple parameters in the array function which are optional lets us see an example for one of the parameter

# Creating an array
import numpy as np
n=np.array([[1,2,3],[4,5,6],[7,8,9]],dtype=complex)
print(n)

Output:

[[1.+0.j 2.+0.j 3.+0.j]
[4.+0.j 5.+0.j 6.+0.j]
[7.+0.j 8.+0.j 9.+0.j]]

Here in the above example a parameter known as dtype is used which is used to specify the data type of the elements in the array like there are multiple other parameters too. The parameters are:

  • Object
  • Copy
  • Order
  • Subok
  • Ndmin

Accessing the array elements

To access the elements of an array a concept known as indexing is used where each element has an index value. Always the starting element has an index value of 0 by default. The last element has an index value of n-1 where n represents the number of elements.

For example


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


Here index value of 1 is 0 and there are 13 elements hence the index value of 13 is 12. This same concept is used in multi-dimensional array let us see that with an example

a=[[1  2  3]

     [4  5  6]

     [7  8  9]]

Here to access element 5, the index should be given as a[1,1] where the first value represents the row value and the second value represents the column value so a[1,1] represents the second row and the second column since the starting value of the index starts with 0  this is how the values are accessed in a multi-dimensional array.

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

Output:

5

Access a NumPy array by column in Python

Let us create an array using the package Numpy and access its columns

# Creating an array
import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])

Now let us access the elements column-wise. In order to access the elements in a column-wise manner colon(:) symbol is used let us see that with an example

import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a[:,1])

Output:

[2 5 8]

Here we can see that the second column values are printed. This is how we access column elements in a NumPy array. We can also modify the code for printing all the column elements using a for loop for example

import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
for i in range(0,3):
    print(a[:,i])

Output:

[1 4 7]

[2 5 8]

[3 6 9]


Note:

In order to access the row elements the syntax is

array_name[row number,:]

This is used to print the row elements.

Leave a Reply

Your email address will not be published.