The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Mean – Get Average pd.DataFrame.mean()

Calculate mean in pandas

You’re anything but average! Jokes aside, Pandas Mean is a fundamental function that is in every data scientist’s, analyst’s, and data monkey’s toolkit.

pandas.DataFrame.mean()
pandas.Series.mean()

Pandas Mean will return the average of your data across a specified axis. If the function is applied to a DataFrame, pandas will return a series with the mean across an axis. If .mean() is applied to a Series, then pandas will return a scalar (single number).

Pseudo Code: With your Series or DataFrame, return the average of the values across a specified axis

pd.Mean()

No matter what field of data you’re doing, you’re going to need to have a good grasp on mean, median, and mode. With mean, python will return the average value of your data.

You must choose which axis you want to average, but this is a wonderful feature. You can choose across rows or columns.

Mean is also included within Pandas Describe.

Mean Parameters

The most important decision you need to make is with axis β€” Do you want to take the average across rows or columns?

  • axis = You can choose if you want to take the average across columns (axis=’index’ or 0) or rows (axis=’columns’ or 1).
  • skipna (Default: True) = Exclude the NA/null values when computing the result. If you set skipna=False and there is an NA in your data, pandas will return β€œNaN” for your average.
  • level = If you have a multi index, then you can pass the name (or int) of your level to compute the mean.
  • numeric_only: You’ll only need to worry about this if you have mixed data types in your columns. Leave this as default to start.

Now the fun part, let’s take a look at a code sample

Link to code

Official Documentation

On this page