Average for each Column and Row in Pandas DataFrame

You may use the following syntax to get the average for each column and row in pandas DataFrame:

(1) Average for each column:

df.mean(axis=0)

(2) Average for each row:

df.mean(axis=1)

Next, I’ll review an example with the steps to get the average for each column and row for a given DataFrame

Steps to get the Average for each Column and Row in Pandas DataFrame

Step 1: Gather the data

To start, gather the data that needs to be averaged.

For example, I gathered the following data about the commission earned by 3 employees (over the first 6 months of the year):

Excel Table

The goal is to get the average of the commission earned:

  • For each employee over the first 6 months (average by column)
  • For each month across all employees (average by row)

Step 2: Create a DataFrame

Next, create a DataFrame in order to capture the above data in Python:

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Jon Commission': [7000,5500,6000,4500,8000,6000],
        'Maria Commission': [10000,7500,6500,6000,9000,8500], 
        'Olivia Commission': [3000,6000,4500,4500,4000,5500],
        }

df = DataFrame(Data,columns=['Month','Jon Commission','Maria Commission','Olivia Commission'])
print (df) 

Run the code in Python, and you’ll get the following DataFrame:

Pandas DataFrame

Step 3: Get the Average for each Column and Row in Pandas DataFrame

You can then apply the following syntax to get the average for each column:

df.mean(axis=0)

For our example, this is the complete Python code to get the average commission earned for each employee over the 6 first months (average by column):

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Jon Commission': [7000,5500,6000,4500,8000,6000],
        'Maria Commission': [10000,7500,6500,6000,9000,8500], 
        'Olivia Commission': [3000,6000,4500,4500,4000,5500]
        }

df = DataFrame(Data,columns=['Month','Jon Commission','Maria Commission','Olivia Commission'])

av_column = df.mean(axis=0)
print (av_column) 

Run the code, and you’ll get the average commission per employee:

Average of each Column and Row in Pandas DataFrame

Alternatively, you can get the average for each row by using the following syntax:

df.mean(axis=1)

Here is the code that you can use to get the average commission earned for each month across all employees (average by row):

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Jon Commission': [7000,5500,6000,4500,8000,6000],
        'Maria Commission': [10000,7500,6500,6000,9000,8500], 
        'Olivia Commission': [3000,6000,4500,4500,4000,5500],
        }

df = DataFrame(Data,columns=['Month','Jon Commission','Maria Commission','Olivia Commission'], index =['Jan ','Feb ','Mar ','Apr ','May ','Jun '])

av_row = df.mean(axis=1)
print (av_row) 

Once you run the code in Python, you’ll get the average commission earned per month:

Get the Average of each Column and Row in Pandas DataFrame

You may also want to check the following source that explains the steps to get the sum for each column and row in pandas DataFrame.