# Numpy-like reduction of particular dimensions in Pandas DataFrame?

With a numpy multidimensional arrays, it is easy to reduce particular axes with functions such as np.mean or 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: A,B,C, and value. A,B,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 A and 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 groupby call.

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.

• why an overshoot? that's exactly what xarray is designed for...
• Also Dataframe.to_xarray makes the conversion quite straightforward as well. I'm a little unclear why the groupby solution becomes clunky, you can always programmatically build a list of columns to pass to groupby.
• But you've outlined two of them. You can use groupby for efficient column-wise query-like operations, or if you want the efficiency of n-dimensional arrays, use df.to_xarray(). A DataFrame is not an N-D data structure, so there's no reason to expect it to perform like one. Give xarray a whirl - it's a great package!