How to iterate over Columns in Numpy

NumPy provides several methods to iterate over columns in a 2D array. The most common approaches include using nditer() with transpose, array transpose directly, apply_along_axis(), and manual iteration with indexing.

Syntax

Here are the key functions used for column iteration ?

np.nditer(array.T)          # Iterator with transpose
array.T                     # Array transpose  
np.apply_along_axis()       # Apply function along axis
array.shape[1]              # Get number of columns

Using nditer() with Transpose

The nditer() function creates an iterator object that can traverse array elements. Combined with transpose, it iterates through each column ?

import numpy as np

# Create a sample 2D array
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90], [110, 120, 130]])
print("Original array:")
print(arr)
print("\nIteration over columns using nditer:")

# Iterate over columns using nditer with transpose
for col in np.nditer(arr.T):
    print(col)
Original array:
[[ 10  20  30]
 [ 40  50  60]
 [ 70  80  90]
 [110 120 130]]

Iteration over columns using nditer:
10
40
70
110
20
50
80
120
30
60
90
130

Using Array Transpose

The transpose operation converts rows to columns, allowing direct iteration over columns as complete arrays ?

import numpy as np

# Create a 3x3 matrix
arr = np.array([[1, 2, 3],
                [4, 5, 6], 
                [7, 8, 9]])

print("Iterating over columns using transpose:")
# Transpose and iterate over each column
for col in arr.T:
    print(col)
Iterating over columns using transpose:
[1 4 7]
[2 5 8]
[3 6 9]

Using apply_along_axis() Function

The apply_along_axis() function applies a given function to 1D slices along a specified axis ?

import numpy as np

# Create a 3x3 array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Function to process each column
def process_column(col):
    print(f"Column: {col}")
    return col

print("Using apply_along_axis to iterate columns:")
# Apply function along axis 0 (columns)
np.apply_along_axis(process_column, 0, arr)
Using apply_along_axis to iterate columns:
Column: [1 4 7]
Column: [2 5 8]
Column: [3 6 9]

Using Manual Indexing

You can manually iterate through columns using array indexing and the shape property ?

import numpy as np

# Create a 3x3 array
arr = np.array([[1, 5, 9], [13, 14, 21], [25, 29, 33]])

# Get the number of columns
num_cols = arr.shape[1]

print("Manual iteration over columns:")
# Iterate through each column index
for col_index in range(num_cols):
    col = arr[:, col_index]  # Extract column
    print(f"Column {col_index}: {col}")
Manual iteration over columns:
Column 0: [ 1 13 25]
Column 1: [ 5 14 29]
Column 2: [ 9 21 33]

Comparison of Methods

Method Returns Best For
nditer() Individual elements Element-wise operations
arr.T Complete columns Column-wise operations
apply_along_axis() Function results Applying functions to columns
Manual indexing Complete columns When you need column indices

Conclusion

Use arr.T for simple column iteration, apply_along_axis() for applying functions to columns, and manual indexing when you need both column data and indices. Choose the method based on whether you need individual elements or complete columns.

Updated on: 2026-03-27T12:45:42+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements