Calculate the Average for each Row in a Pandas DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Calculate the Average for each Row in a Pandas DataFrame
  2. Calculate the Row Average in a Pandas DataFrame using df.iloc
  3. Calculating the row average based on the column names in a Pandas DataFrame

# Calculate the Average for each Row in a Pandas DataFrame

To calculate the average for each row in a Pandas DataFrame:

  1. Call the mean() method on the DataFrame.
  2. Set the axis argument to 1 to calculate the average for each row.
main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) df['row_average'] = df.mean(axis=1) print('-' * 50) print(df['row_average'])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- 0 30.0 1 40.0 2 50.0 Name: row_average, dtype: float64

calculate the average for each row in pandas dataframe

The DataFrame.mean() method returns the mean of the values over the specified axis.

By default, the axis argument is set to 0, which means that the column average is calculated.

We set the axis argument to 1 to compute the mean along the rows' axis.

main.py
df['row_average'] = df.mean(axis=1)

When running the code sample you might get a warning that "A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead".

If you get the warning, try to use the DataFrame.assign() method when adding the row average column to the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) df = df.assign(row_average=df.mean(axis=1)) print('-' * 50) print(df['row_average'])
The code for this article is available on GitHub

Running the code sample produces the same output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- 0 30.0 1 40.0 2 50.0 Name: row_average, dtype: float64

calculate row average using dataframe assign

# Calculate the Row Average in a Pandas DataFrame using df.iloc

If you only want to calculate the row average for specific columns, use the df.iloc position-based indexer.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) df['row_average'] = df.iloc[:, 0:2].mean(axis=1) print('-' * 50) print(df['row_average'])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- 0 20.0 1 30.0 2 40.0 Name: row_average, dtype: float64

calculate row average using df iloc

We used the df.iloc indexer to calculate the row average for the A and B columns.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) print('-' * 50) print(df.iloc[:, 0:2])

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- A B 0 10 30 1 20 40 2 30 50

Once you select the specific columns, you just have to call the mean() method with the axis argument set to 1.

main.py
df['row_average'] = df.iloc[:, 0:2].mean(axis=1)

You can also use the df.iloc position-based indexer to specify individual columns by index before calculating the row average.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) df['row_average'] = df.iloc[:, [1, 2]].mean(axis=1) print('-' * 50) print(df['row_average'])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- 0 40.0 1 50.0 2 60.0 Name: row_average, dtype: float64

get row average for specific columns in pandas dataframe

We calculated the row average for the columns with indexes 1 and 2.

Note that Python indices are zero-based, so the first index is 0 and the last is len(columns) - 1.

# Calculating the row average based on the column names in a Pandas DataFrame

You can use the DataFrame.loc label-based indexer to calculate the row average based on specific column names.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) df['row_average'] = df.loc[:, ['B', 'C']].mean(axis=1) print('-' * 50) print(df['row_average'])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- 0 40.0 1 50.0 2 60.0 Name: row_average, dtype: float64

calculate row average based on column names

The df.loc indexer is label-based, so it enables you to select specific columns based on their name.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30], 'B': [30, 40, 50], 'C': [50, 60, 70] }) print(df) print('-' * 50) print(df.loc[:, ['B', 'C']])
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
A B C 0 10 30 50 1 20 40 60 2 30 50 70 -------------------------------------------------- B C 0 30 50 1 40 60 2 50 70

select specific columns by name before calculating row average

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.