如何找到多索引数据帧每个级别的最大绝对值?
数据如下所示:
data
a b c
1 X 1 -2
X 2 +2
2 X 1 -1
X 2 +2
Y 1 +6
3 X 1 -5
Y 1 -3
Y 2 +5
以下是我在保持多索引的第 1 级和第 2 级时期望收到的内容:
data
a b
1 X 2
2 X 2
Y 6
3 X -5
Y +5
使用idxmax
:
idx = df['data'].abs().groupby(level=[0,1]).idxmax()
df.loc[idx]
结果:
data
a b c
1 X 1 -2
2 X 2 2
Y 1 6
3 X 1 -5
Y 2 5
外植:
idx = df['data'].abs() # convert the `data` column to its absolute value
.groupby(level=[0,1]) # group by the first two levels (`a` and `b`)
.idxmax() # find the index of the row with max value in each group
df.loc[idx] # get the rows at indexes `idx`