With a numpy multidimensional arrays, it is easy to reduce particular axes with functions such as
np.sum. For example,
np.mean(X, axis=2,keepdims=False) will eliminate the second axis.
What if our data is in Pandas long-format DataFrame and we want to reduce a particular variable in a similar fashion?
For example, consider a dataset with four columns:
C specify independent variables, and
value specifies a dependent measure. If we wish to reduce the dataframe by eliminating
B (i.e., averaging across all levels of
B, but not within levels of
C), we can use
df.groupby(['A','C']).mean(). However, this solution becomes very clunky if there are many independent variables that we must enumerate in the
Does pandas have an efficient method for achieving numpy-like variable reduction? In principle, one can convert the DataFrame into an xarray, but this seems like an overshoot.