如何选择某些列具有最大值之一的行

时间:2021-05-14 11:41:56

标签: python pandas

我有一个 Pandas 数据框,想选择某些列在 2 个最大值中的行。输出应显示“持续时间”为 50 和 45 的行

我试过了

data = {
  "production": [420, 380, 390],
  "duration": [50, 40, 45]
}

df = pd.DataFrame(data)

df[df['production'] == df['production'].nlargest(2)]
<块引用>

ValueError: 只能比较标记相同的系列对象

1 个答案:

答案 0 :(得分:2)

尝试:

result = df[df['production'].isin(df['production'].nlargest(2))]

或者,如果您想要位于这两个值内的所有人口:

result = df[df['production'].between(*df['production'].nlargest(2).values[::-1])]
相关问题