获取具有多个相同数据的数据框中的最佳值

时间:2018-05-04 09:05:11

标签: python pandas dataframe

例如,我有df

                   score
0    a    b    c    0.7
1    a    b    c    0.7
2    b    c    d    0.8
3    c    d    e    0.9
4    c    d    e    0.9
5    d    e    f    0.8

我想获取得分最高的值,但有多个数据。我想采取最后的最好(基于例子,意味着第4行)。你能帮我吗?提前谢谢。

2 个答案:

答案 0 :(得分:3)

您可以locscore查找最大值,然后iloc选择最后一行:

res = df.loc[df['score'] == df['score'].max()]\
        .iloc[-1]

print(res)

score    0.9
Name: (4, c, d, e), dtype: float64

答案 1 :(得分:1)

您可以使用max方法查找最高分数。

max_score = df['score'].max()

然后选择具有最高分数的行。

df = df[df.score == max_score].iloc[-1]

输出

               score
4  c   d   e    0.9
相关问题