Wednesday, June 17, 2026
HomeLanguagesCalculate the mean across dimension in a 2D NumPy array

Calculate the mean across dimension in a 2D NumPy array

We can find out the mean of each row and column of 2d array using numpy with the function np.mean(). Here we have to provide the axis for finding mean.

Syntax: numpy.mean(arr, axis = None)

For Row mean: axis=1

For Column mean: axis=0

Example:

Python3




# Importing Library
import numpy as np
  
# creating 2d array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  
# Calculating mean across Rows
row_mean = np.mean(arr, axis=1)
  
row1_mean = row_mean[0]
print("Mean of Row 1 is", row1_mean)
  
row2_mean = row_mean[1]
print("Mean of Row 2 is", row2_mean)
  
row3_mean = row_mean[2]
print("Mean of Row 3 is", row3_mean)
  
  
# Calculating mean across Columns
column_mean = np.mean(arr, axis=0)
  
column1_mean = column_mean[0]
print("Mean of column 1 is", column1_mean)
  
column2_mean = column_mean[1]
print("Mean of column 2 is", column2_mean)
  
column3_mean = column_mean[2]
print("Mean of column 3 is", column3_mean)


Output:

Mean of Row 1 is 2.0
Mean of Row 2 is 5.0
Mean of Row 3 is 8.0
Mean of column 1 is 4.0
Mean of column 2 is 5.0
Mean of column 3 is 6.0
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS