如何在 Pandas 的 DataFrame 的列中提取第 n 个最大值/最小值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48026397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:59:13  来源:igfitidea点击:

How to extract the n-th maximum/minimum value in a column of a DataFrame in pandas?

pythonpandasdataframemaxmin

提问by Krzysztof S?owiński

I would like to obtain the n-th minimum or the n-th maximum value from numerical columns in the DataFramein pandas.

我想从DataFramein pandas 的数字列中获得第 n 个最小值或第 n 个最大值。

Example:

例子:

df = pd.DataFrame({'a': [3.0, 2.0, 4.0, 1.0],'b': [1.0, 4.0 , 2.0, 3.0]})

     a    b
0  3.0  1.0
1  2.0  4.0
2  4.0  2.0
3  1.0  3.0

The third largest value in column ais 2 and the second smallest value in column bis also 2.

列中第三大的值a是 2 ,列中第二小的值b也是 2。

回答by cs95

You can use nlargest/nsmallest-

您可以使用nlargest/ nsmallest-

df    
     a    b
0  3.0  1.0
1  2.0  4.0
2  4.0  2.0
3  1.0  3.0
df.a.nlargest(3).iloc[-1]
2.0

Or,

或者,

df.a.nlargest(3).iloc[[-1]]

1    2.0
Name: a, dtype: float64

And, as for b-

而且,至于b——

df.b.nsmallest(2).iloc[-1]
2.0

Or,

或者,

df.b.nsmallest(2).iloc[[-1]]

2    2.0
Name: b, dtype: float64

Quick observation here - this sort of operation cannot be vectorised. You are essentially performing two completely different operations here.

在这里快速观察 - 这种操作不能矢量化。您在这里实际上是在执行两个完全不同的操作。

回答by nbm

df =  
     a    b
0  3.0  1.0
1  2.0  4.0
2  4.0  2.0
3  1.0  3.0

df.nlargest(3,'a')
   =2.0

df.nsmallest(2,'b')=2.0