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 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