October 2017
Intermediate to advanced
532 pages
16h 10m
English
In this recipe, we returned a single row as a Series for each group. It's possible to return any number of rows and columns for each group by returning a DataFrame. In addition to finding just the arithmetic and weighted means, let's also find the geometric and harmonic means of both SAT columns and return the results as a DataFrame with rows as the name of the type of mean and columns as the SAT type. To ease the burden on us, we use the NumPy function average to compute the weighted average and the SciPy functions gmean and hmean for geometric and harmonic means:
>>> from scipy.stats import gmean, hmean>>> def calculate_means(df): df_means = pd.DataFrame(index=['Arithmetic', 'Weighted', 'Geometric', 'Harmonic']) cols = ['SATMTMID', ...
Read now
Unlock full access