Wednesday, February 4, 2026
HomeLanguagesProgram to access different columns of a multidimensional Numpy array

Program to access different columns of a multidimensional Numpy array

Prerequisite: Numpy module

The following article discusses how we can access different columns of multidimensional Numpy array. Here, we are using Slicing method to obtain the required functionality. 

Example 1: (Accessing the First and Last column of Numpy array)

Python3




# Importing Numpy module
import numpy as np
  
# Creating a 3x3 Numpy array
arr = np.array([[11, 20, 3], 
                [89, 5, 66], 
                [71, 88, 39]])
  
print("Given Array :")
print(arr)
  
# Access the First and Last column of array
res_arr = arr[:,[0,2]]
print("\nAccessed Columns :")
print(res_arr)


Output:

Given Array :

[[11 20  3]

[89  5 66]

[71 88 39]]

Accessed Columns :

[[11  3]

[89 66]

[71 39]]

Example 2: (Accessing the Middle and Last column of Numpy array)

Python3




# Importing Numpy module
import numpy as np
  
# Creating a 4x4 Numpy array
arr = np.array([[1, 20, 3, 1], 
                [40, 5, 66, 7], 
                [70, 88, 9, 11],
               [80, 100, 50, 77]])
  
print("Given Array :")
print(arr)
  
# Access the Middle and Last column of array
res_arr = arr[:,[1,3]]
print("\nAccessed Columns :")
print(res_arr)


Output:

Given Array :

[[  1  20   3   1]

[ 40   5  66   7]

[ 70  88   9  11]

[ 80 100  50  77]]

Accessed Columns :

[[ 20   1]

[  5   7]

[ 88  11]

[100  77]]

Example 3: (Accessing the Last two columns of Numpy array)

Python3




# Importing Numpy module
import numpy as np
  
# Creating a 3d (3X4X4) Numpy array
arr = np.array([[[21, 20, 3, 1], 
                [40, 5, 66, 7], 
                [70, 88, 9, 11],
               [80, 100, 50, 77]],
  
               [[65, 120, 53, 73], 
                [49, 50, 56, 11], 
                [81, 88, 34, 22],
               [564,56, 76, 99]],
                 
               [[45, 85, 38, 455], 
                [40, 53, 69, 6], 
                [50, 528, 654, 11],
               [54, 87, 78, 77]]])
  
print("Given Array :")
print(arr)
  
# Access the Last two columns of array
res_arr = arr[2,:,[2,3]]
print("\nAccessed Columns :")
print(res_arr)


Output:

Given Array :

[[[ 21  20   3   1]

 [ 40   5  66   7]

 [ 70  88   9  11]

 [ 80 100  50  77]]

[[ 65 120  53  73]

 [ 49  50  56  11]

 [ 81  88  34  22]

 [564  56  76  99]]

[[ 45  85  38 455]

 [ 40  53  69   6]

 [ 50 528 654  11]

 [ 54  87  78  77]]]

Accessed Columns :

[[ 38  69 654  78]

[455   6  11  77]]

Example 4: (Accessing the First column of a 4D Numpy array)

Python3




# Importing Numpy module
import numpy as np
  
# Creating a 4D Numpy array
arr = np.array([
  [
    [
      [1,2],
      [3,4]
    ],
    [
      [5,6],
      [7,8]
    ]
  ],
   [
    [
      [9,10],
      [11,12]
    ],
    [
      [13,14],
      [15,16]
    ]
  ]
  
])
  
print("Given Array :")
print(arr)
  
# Access the First three columns of array
res_arr = arr[0,0,:,[0]]
print("\nAccessed Columns :")
print(res_arr)


Output:

Given Array :

[[[[ 1  2]

  [ 3  4]]

 [[ 5  6]

  [ 7  8]]]

[[[ 9 10]

  [11 12]]

 [[13 14]

  [15 16]]]]

Accessed Columns :

[[1 3]]

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
125 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11980 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6936 POSTS0 COMMENTS
Umr Jansen
6919 POSTS0 COMMENTS