python pandas-确定面板中滚动最大值的行?
面板是否有类似于pan.idxmax(axis='items')的东西,可以返回行或索引以获得最大值,如下面链接的问题所示,piRSquared完美地回答了这个问题python pandas-确定面板中滚动最大值的行?,python,pandas,max,panel,vectorization,Python,Pandas,Max,Panel,Vectorization,面板是否有类似于pan.idxmax(axis='items')的东西,可以返回行或索引以获得最大值,如下面链接的问题所示,piRSquared完美地回答了这个问题 考虑一下pd.面板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
考虑一下
pd.面板
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)
做得很好,皮。非常感谢。如果你想用物品名称替换物品#,你会用dict映射回名称吗?@MJS类似的,是的。
idxmax(p, 1)
idxmax(p, 2)