python pandas - 从面板中识别滚动最大值行?

python pandas - identify row of rolling maximum value from panel?

Panel 是否有类似 pan.idxmax(axis='items') 的东西,它可以 return 行 # 或最大值的索引,如下面链接的问题中 piRSquared 完美回答?

考虑 pd.Panel p

dfs = dict(
    one=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
    two=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
    three=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
)

p = pd.Panel(dfs)
p.to_frame().unstack()


pd.Panel 对象没有 idxmax 方法。但是底层的 numpy 数组确实有 argmax 方法。我已经构建了这个自定义函数来利用它。

def idxmax(pn, axis=0):
    indices = pd.Series(['items', 'major_axis', 'minor_axis'])
    idx, col = indices.drop(axis)
    return pd.DataFrame(pn.values.argmax(axis),
                        pn.__getattribute__(idx),
                        pn.__getattribute__(col))

用法

idxmax(p, 0)

idxmax(p, 1)

idxmax(p, 2)