获取pandas groupby中元组值列的idxmax或idxmin

时间:2019-02-12 19:38:49

标签: python pandas numpy pandas-groupby

我有一个元组值的分数,我想获取与最大值对应的行。我想做的一个玩具例子是:

    LOGIN_REDIRECT_URL = "book:author" 

import pandas as pd df = pd.DataFrame({'id': ['a', 'a', 'b', 'b'], 'score': [(1,1,1), (1,1,2), (0, 0, 100), (8,8,8)], 'numeric_score': [1, 2, 3, 4], 'value':['foo', 'bar', 'baz', 'qux']}) # Works, gives correct result: correct_df = df.loc[df.groupby('id')['numeric_score'].idxmax(), :] # Fails with a TypeError goal_df = df.loc[df.groupby('id')['score'].idxmax(), :] correct_df中具有我想要的结果。这会引发很多错误,其核心似乎是:

goal_df

一个可行但丑陋的解决方案是:

TypeError: reduction operation 'argmax' not allowed for this dtype

这有光滑的版本吗?

1 个答案:

答案 0 :(得分:1)

我了解您的问题是

“ NumPy的.argmax()不适用于元组。对于一系列元组,我如何确定最大值的元组的索引?”

IIUC,这将返回预期的结果:

df.loc[df.score == df.score.max()]