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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.