Mean or average in python
The (arithmetic) mean is calculated using the formula
\begin{equation*} \bar{x} = \frac{\sum_i x_i}{\sum_i 1}. \end{equation*}
In python, we can use any either numpy, pandas or statistics.
Below, I show their use case for the given data:
If we have a set of values, e.g.
| 100 |
| 50 |
| 30 |
| 65 |
| 76 |
| 93 |
| 53 |
| 28 |
| 61 |
| 7 |
NOTE that the data in our example is a column array.
print(f"Values: \n {values}")
Values: [[100], [50], [30], [65], [76], [93], [53], [28], [61], [7]]
import numpy as np print(f"Numpy Mean: {np.array(values).mean()}")
Numpy Mean: 56.3
import pandas as pd print(f"Pandas Mean: {pd.DataFrame(values).mean()}")
Pandas Mean: 0 56.3 dtype: float64
import statistics import numpy as np values = np.array(values).flatten().astype(float) print(f"Statistics Mean: {statistics.mean(values)}")
Statistics Mean: 56.3
Concluding remarks
numpy and pandas excel in calculating the mean from a column array data, and the result is a float despite the original data is of int datatype. On the other hand, the mean from the statistics module requires a flatten array, and by default doesn't cast the result to a float.