Average each Column and Row in Pandas DataFrame

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

(1) Average of each column:

df.mean(axis=0)

(2) Average of each row:

df.mean(axis=1)

Next, you’ll see an example with the steps to get the average of each column and row for a given DataFrame.

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

Step 1: Prepare the data

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

For example, let’s suppose that you have the following data about the commission earned by 3 people (over the first 6 months of the year):

month a_commission  b_commission  c_commission 
Jan 7000 10000 3000
Feb 5500 7500 6000
Mar 6000 6500 4500
Apr 4500 6000 4500
May 8000 9000 4000
Jun 6000 8500 5500

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

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

Step 2: Create a DataFrame

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

import pandas as pd

data = {'month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'a_commission': [7000,5500,6000,4500,8000,6000],
        'b_commission': [10000,7500,6500,6000,9000,8500], 
        'c_commission': [3000,6000,4500,4500,4000,5500]
        }

df = pd.DataFrame(data)
print (df)

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

  month  a_commission  b_commission  c_commission
0  Jan           7000         10000          3000
1  Feb           5500          7500          6000
2  Mar           6000          6500          4500
3  Apr           4500          6000          4500
4  May           8000          9000          4000
5  Jun           6000          8500          5500

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

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

df.mean(axis=0)

Here is the complete Python code to get the average commission earned by each person over the 6 first months (average by the column):

import pandas as pd

data = {'month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'a_commission': [7000,5500,6000,4500,8000,6000],
        'b_commission': [10000,7500,6500,6000,9000,8500], 
        'c_commission': [3000,6000,4500,4500,4000,5500]
        }

df = pd.DataFrame(data)

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

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

a_commission    6166.666667
b_commission    7916.666667
c_commission    4583.333333

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

df.mean(axis=1)

Here is the code to get the average commission earned for each month across all people (average by the row):

import pandas as pd

data = {'month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'a_commission': [7000,5500,6000,4500,8000,6000],
        'b_commission': [10000,7500,6500,6000,9000,8500], 
        'c_commission': [3000,6000,4500,4500,4000,5500]
        }

df = pd.DataFrame(data, 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:

Jan     6666.666667
Feb     6333.333333
Mar     5666.666667
Apr     5000.000000
May     7000.000000
Jun     6666.666667

You may also want to check the following guide that explains the steps to get the sum of each column and row in Pandas DataFrame.