Yury Bayda
Yury Bayda

Reputation: 1063

Calculate minimum value for each column of multi-indexed DataFrame in pandas

I have a multi-indexed DataFrame with the following structure:

         metric1                 metric2            
     experiment1 experiment2 experiment1 experiment2
run1         1.2         1.5         0.2         0.9
run2         2.1         0.7         0.4         4.3

How can I calculate minimum (maximum, mean, etc.) value for each column and get DataFrame like this:

         metric1                 metric2            
     experiment1 experiment2 experiment1 experiment2
run1         1.2         1.5         0.2         0.9
run2         1.6         0.9         0.3         3.1
run3         2.1         0.7         0.4         4.3
min          1.2         0.7         0.2         0.9
max          2.1         1.5         0.4         4.3

Upvotes: 0

Views: 2620

Answers (1)

Brian from QuantRocket
Brian from QuantRocket

Reputation: 5468

You can take the min, max, and mean then use pd.concat to stitch everything together. You'll need to transpose (T) then transpose back to get the dataframe to concat the way you want.

In [91]: df = pd.DataFrame(dict(exp1=[1.2,2.1],exp2=[1.5,0.7]), index=["run1", "run2"])

In [92]: df_min, df_max, df_mean = df.min(), df.max(), df.mean()

In [93]: df_min.name, df_max.name, df_mean.name = "min", "max", "mean"

In [94]: pd.concat((df.T, df_min, df_max, df_mean), axis=1).T
Out[94]:
      exp1  exp2
run1  1.20   1.5
run2  2.10   0.7
min   1.20   0.7
max   2.10   1.5
mean  1.65   1.1

Should work the same with a multi-index.

Upvotes: 4

Related Questions