我有一个分组的熊猫DataFrame,如下所示:
Host 1: master_forest_01 replica_forest_03
Host 2: master_forest_02 replica_forest_01
Host 3: master_forest_03 replica_forest_02
我想为每个组获取并支持每列的最高值,以及相关的模型和节点。那就是:
Fx Fy Fz Mx My Mz
Model Group Support Node
Reactions 00 G P1 1 -3.0 3.0 170.0 0.0 -23.0 -1.0
P2 3 6.0 11.0 180.0 0.0 -15.0 -1.0
4 -1.0 19.0 115.0 0.0 -7.0 -1.0
Q P1 1 -1.0 7.0 550.0 0.0 -19.0 -1.0
P2 3 2404.0 15.0 2005.0 0.0 -11.0 -1.0
4 2736.0 23.0 -2995.0 0.0 -3.0 -1.0
Reactions 01 D P1 1 -1.0 -9.0 -1.0 0.0 23.0 -1.0
P2 3 -7.0 -19.0 140.0 0.0 13.0 1.0
4 -5.0 -29.0 -3950.0 0.0 3.0 1.0
G P1 1 -1.0 -1.0 10.0 0.0 15.0 -1.0
P2 3 -6.0 -6.0 10.0 0.0 10.0 6.0
4 4.0 -11.0 100.0 0.0 5.0 -11.0
Q P1 1 0.0 -5.0 400.0 0.0 27.0 -1.0
P2 3 -86.0 -15.0 -95.0 0.0 17.0 1.0
4 -110.0 -25.0 135.0 0.0 7.0 -1.0
对于从Fx到Mz的每列,依此类推。
答案 0 :(得分:2)
您可以按索引级别分组,并调用自定义lambda
函数。将这些lambda
作为元组传递来控制列标题,并使用idxmax
聚合函数:
df.groupby(level=[1, 2]).agg(['max',
('model', lambda x: x.idxmax()[0]),
('node', lambda x: x.idxmax()[3])])
[出]
Fx Fy Fz \
max model node max model node max
Group Support
D P1 -1.0 Reactions 01 1.0 -9.0 Reactions 01 1.0 -1.0
P2 -5.0 Reactions 01 4.0 -19.0 Reactions 01 3.0 140.0
G P1 -1.0 Reactions 01 1.0 3.0 Reactions 00 1.0 170.0
P2 6.0 Reactions 00 3.0 19.0 Reactions 00 4.0 180.0
Q P1 0.0 Reactions 01 1.0 7.0 Reactions 00 1.0 550.0
P2 2736.0 Reactions 00 4.0 23.0 Reactions 00 4.0 2005.0
Mx My \
model node max model node max model
Group Support
D P1 Reactions 01 1.0 0.0 Reactions 01 1.0 23.0 Reactions 01
P2 Reactions 01 3.0 0.0 Reactions 01 3.0 13.0 Reactions 01
G P1 Reactions 00 1.0 0.0 Reactions 00 1.0 15.0 Reactions 01
P2 Reactions 00 3.0 0.0 Reactions 00 3.0 10.0 Reactions 01
Q P1 Reactions 00 1.0 0.0 Reactions 00 1.0 27.0 Reactions 01
P2 Reactions 00 3.0 0.0 Reactions 00 3.0 17.0 Reactions 01
Mz
node max model node
Group Support
D P1 1.0 -1.0 Reactions 01 1.0
P2 3.0 1.0 Reactions 01 3.0
G P1 1.0 -1.0 Reactions 00 1.0
P2 3.0 6.0 Reactions 01 3.0
Q P1 1.0 -1.0 Reactions 00 1.0
P2 3.0 1.0 Reactions 01 3.0