How to Set Axis for Rows and Columns in NumPy ? Last Updated : 07 Nov, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to set the axis for rows and columns in NumPy. Functions Usednp.array(object): to create a NumPy array, the object is the parameter that contains the arraynp.reshape(rows, columns): to reshape the array into the specified number of rows and columns. Here in the below examples, we have given -1 in place of rows to let numpy figure it out if there are 3 columns in each row.np.sum(axis): to calculate the sum or addition of the elements. Here we have mentioned the axis to do the operation array-wise, row-wise, or column-wise as per requirement. Example 1: Set axis for array-wise calculation In this example, we will reshape the NumPy array into rows having 3 columns each i.e nparray.reshape(-1, 3) to make it two-dimensional. Then we will perform the sum operation of the array elements array-wise that is in normal order starting from the first to last element of the NumPy array. We specifically set the axis= None to trigger the normal array-wise operation. Code: Python3 import numpy as np nparray = np.array([[1, 2, 3], [11, 22, 33], [4, 5, 6], [8, 9, 10], [20, 30, 40]]) nparray = nparray.reshape(-1, 3) print(nparray) # calculating sum along # axix=None i.e array-wise output = nparray.sum(axis=None) print("\n\nSum array-wise: ", output) Output : [[ 1 2 3] [11 22 33] [ 4 5 6] [ 8 9 10] [20 30 40]] Sum array-wise: 204 Example 2: Set axis for column-wise calculation In this example, we will reshape the numpy array into rows having 3 columns each. Then perform the sum operation of the array elements using the sum() function column-wise. We specifically set the axis= 0 to trigger the normal array-wise operation. Code: Python3 import numpy as np nparray = np.array([[1, 2, 3], [11, 22, 33], [4, 5, 6], [8, 9, 10], [20, 30, 40]]) nparray = nparray.reshape(-1, 3) print(nparray) # calculating sum along axix=0 # i.e column-wise output = nparray.sum(axis = 0) print("\n\nSum column-wise: ", output) Output : [[ 1 2 3] [11 22 33] [ 4 5 6] [ 8 9 10] [20 30 40]] Sum column-wise: [44 68 92] Example 3: Set axis for row-wise calculation We will specifically set the axis = 1 to trigger the normal row-wise calculation. Code: Python3 import numpy as np nparray = np.array([[1, 2, 3], [11, 22, 33], [4, 5, 6], [8, 9, 10], [20, 30, 40]]) nparray = nparray.reshape(-1, 3) print(nparray) # calculating sum along axix=1 # i.e row0wise output = nparray.sum(axis = 1) print("\n\nSum row-wise: ", output) Output : [[ 1 2 3] [11 22 33] [ 4 5 6] [ 8 9 10] [20 30 40]] Sum row-wise: [ 6 66 15 27 90] Comment More infoAdvertise with us Next Article How to Set Axis for Rows and Columns in NumPy ? rijushree100guha Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads How to Swap Two Rows in a NumPy Array One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a N 4 min read How to access a NumPy array by column Accessing a NumPy-based array by a specific Column index can be achieved by indexing. NumPy follows standard 0-based indexing in Python.  Example:Given array: 1 13 6 9 4 7 19 16 2 Input: print(NumPy_array_name[ :,2]) Output: [6 7 2] Explanation: printing 3rd columnAccess ith column of a 2D Numpy Arr 3 min read How to Use axis=0 and axis=1 in Pandas? In this article, we will discuss how to use axis=0 and axis=1 in pandas using Python. Sometimes we need to do operations only on rows, and sometimes only on columns, in such situations, we specify the axis parameter. In this article, let's see a few examples to know when and how to use the axis para 2 min read How to Add Multiple Axes to a Figure in Python In this article, we are going to discuss how to add multiple axes to a figure using matplotlib in Python. We will use the add_axes() method of matplotlib module to add multiple axes to a figure. Step-by-step Approach: Import required modulesAfter importing matplotlib, create a variable fig and equal 3 min read Matplotlib.axis.Axis.set_smart_bounds() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.set_smart_bounds() Function The Axis.set_smart_bounds() fu 2 min read Matplotlib.axes.Axes.imshow() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. 3 min read Find a matrix or vector norm using NumPy To find a matrix or vector norm we use function numpy.linalg.norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters. Syntax: numpy.linalg.norm(x, ord=None, axis=None)Parameters: x: input ord: 2 min read How to Set X-Axis Values in Matplotlib in Python? In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) xticks() functio 2 min read Matplotlib.axis.Axis.grid() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.grid() Function The Axis.grid() function in axis module of 2 min read Matplotlib.axes.Axes.format_coord() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. 2 min read Like