Numpy (abbreviation for ‘Numerical Python‘) is a library for performing large scale mathematical operations in fast and efficient manner. This article serves to educate you about methods one could use to iterate over columns in an 2D
NumPy array. Since a single dimensional array only consists of linear elements, there doesn’t exists a distinguished definition of rows and columns in them. Therefore, in order to perform such operations we need a array whose
len(ary.shape) > 1 .
NumPy on your python environment, type the following code in your OS’s Command Processor (CMD, Bash etc):
pip install numpy
We would be taking a look at several methods of iterating over a column of an Array/Matrix:-
CODE: Use of primitive 2D Slicing operation on an array to get the desired column/columns
[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) [ 0 5 10 15 20] [ 1 6 11 16 21] [ 2 7 12 17 22] [ 3 8 13 18 23] [ 4 9 14 19 24]
In the above code, we firstly create an linear array of 25 elements (0-24) using
np.arange(25). Then we reshape (transform 1D to 2D) using
np.reshape() to create a 2D array out of a linear array. Then we output the transformed array. Now we used a for loop which would iterate x times (where x is the number of columns in the array) for which we used
range() with the argument
shape = number of columns in a 2D symmetric array). In each iteration we output a column out of the array using
ary[:, col] which means that give give all elements of the column number =
In this method we would transpose the array to treat each column element as a row element (which in turn is equivalent of column iteration).
[ 0 5 10 15 20] [ 1 6 11 16 21] [ 2 7 12 17 22] [ 3 8 13 18 23] [ 4 9 14 19 24]
Firstly, we created an 2D array (same as the previous example) using
np.array() and initialized it with 25 values. Then we transposed the array, using
ary.T which in turn switches the rows with the columns and columns with the rows. Then we iterated over each row of this transposed array and printed the row values.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Python Iterate over multiple lists simultaneously
- Iterate over a dictionary in Python
- Iterate over a set in Python
- Iterate over characters of a string in Python
- Iterate over words of a String in Python
- How to Iterate over Dataframe Groups in Python-Pandas?
- Iterate over a list in Python
- Different ways to iterate over rows in Pandas Dataframe
- How to iterate over rows in Pandas Dataframe
- Iterating over rows and columns in Pandas DataFrame
- Python | Ways to iterate tuple list of lists
- Python | Iterate through value lists dictionary
- Python - Iterate through list without using the increment variable
- Python | Ways to add row/columns in numpy array
- Benefit of NumPy arrays over Python arrays
- How to swap columns of a given NumPy array?
- Find the number of rows and columns of a given matrix using NumPy
- Calculating the sum of all columns of a 2D NumPy array
- Calculate the sum of all columns in a 2D NumPy array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.