# How to iterate over Columns in Numpy

The name Numpy stands for Numerical Python. It helps to solve the mathematical operation on an array. In Python, we have some built-in functions such as nditor(), T(), array(), shape[], and apply_along_axis() that will be used to iterate over columns in Numpy.

### Syntax

The following syntax is used in the examples-

nditer()


The NumPy module contains this built-in function for iterator objects.

T()


This function refers to transposing of the index in a column dataframe.

array()


This is a built-in function in Python that creates the array. An array is defined by collecting the items of the same data type.

apply_along_axis()


The apply_along_axis() runs the given function through 1D slices of the input array, with the slices being taken along the axis we choose.

Shape[]


This is used to fetch the dimension of a module named Numpy. Also, fetch the dimension of Pandas module.

## Using nditer() function

The program uses for loop, where the variable iterate through the built-in function nditer() with T() to iterate over Columns in Numpy.

### Example

In the following example, we will start importing the module named numpy and take the object reference as ny. Then create the 4*3 matrix array by using built-in function array() with ny and store it in the variable arr. Next, use the for loop where variable col iterates over nditer() that accepts the parameter- arr.T() to transpose the index of the column. Finally, use the variable col in the print function to get the result.

import numpy as ny
# Create a sample 2D array
arr = ny.array([[10, 20, 30], [40, 50, 60], [70, 80, 90], [110, 120, 130]])
print("Iteration of all the columns:")
# Iterate over columns using nditer
for col in ny.nditer(arr.T):
print(col)


### Output

 Iteration of all the columns:
10
20
30
40
50
60
70
80
90
110
120
130


## Using Array Transpose

The program uses the concept of matrix where it transposes the matrix by row into column and this way it will iterate over columns in Numpy.

### Example

In the following example, begin the program by importing the module and then create the list matrix by using the built-in function array() to store in the variable arr. Now transpose the input array matrix using for loop where variable col iterates over each column with the help of arr.T. Finally, we are printing the result with the help of variable col.

# import the module
import numpy as np
# create the list matrix
arr = np.array([[1, 2, 3 ],
[4, 5, 6],
[7, 8, 9]]
)
# Transpose array matrix
for col in arr.T:
print(col)


### Output

 [1 4 7]
[2 5 8]
[3 6 9]


## Using apply_along_axis() Function

The program uses the built-in function apply_along_axis() which will slice the given function of the input array to iterate over each column in Numpy.

### Example

In the following example, start importing the module and then create the 2D array representation in the variable arr. Next, operate each column using the user-defined function that accepts the parameter named col that receives the value through the built-in function apply_along_axis() and it will be printing the columns. In the end, the apply_along_matrix() accepts the following parameters-

• p_column: This is the first parameter as named function definition to each column of the transpose array arr.T(third parameter).

• 0: The second argument 0 specifies that the function should be applied along the first axis (columns) of the input array.

• arr.T: Transpose the array that will help to iterate over the Columns.

import numpy as np
# 2D array representation
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# operate each column using the function
def p_column(col):
print(col)
# Iterate the column
np.apply_along_axis(p_column, 0, arr.T)


### Output

[1 2 3]
[4 5 6]
[7 8 9]


## Using While Loop

The program uses the shape method to fetch the dimension of NumPy module and using a while loop it will iterate over each column of the input array.

### Example

In the following example, start importing the module and create the input array to make an array list consisting of rows and columns in the variable arr. Using the same variable it will use Shape[] to fetch the dimension of an array and store it in the variable num_cols. Then initialize the variable col_index to 0 which shows the initial index value. Next, use the while loop to set the condition by using < operator. Then use slicing and incrementing to generate the result.

import numpy as np
# Create the 2D array
arr = np.array([[1, 5, 9], [13, 14, 21], [25, 29, 33]])
# Get the number of columns
num_cols = arr.shape[1]
# Initialize the column index
col_index = 0
# Using a while loop iterates over each column
while col_index < num_cols:
col = arr[:, col_index]
print(col)
col_index += 1


### Output

 [ 1 13 25]
[ 5 14 29]
[ 9 21 33]


## Conclusion

We discussed the various ways to solve this problem statement. We saw some of the popular iteration built-in function of Numpy i.e. nditer(), T(), and, shape[] that helps to iterate over the column. The Numpy module works in the following operation such as- linear algebra, matrices, and, Fourier transform.

Updated on: 16-Aug-2023

336 Views