[英]How to extract the n-th maximum/minimum value in a column of a DataFrame in pandas?
I would like to obtain the n-th minimum or the n-th maximum value from numerical columns in the DataFrame
in pandas.我想从 Pandas 的DataFrame
中的数字列中获取第 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 a
is 2 and the second smallest value in column b
is also 2. a
列中的第三个最大值是 2, b
列中的第二个最小值也是 2。
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.您实际上在这里执行了两个完全不同的操作。
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
5th max: df.a.nlargest(5).min()
第 5 个最大值: df.a.nlargest(5).min()
5th min: df.a.nsmallest(5).max()
.第 5 分钟: df.a.nsmallest(5).max()
。
Tips: add param keep
may acts better:提示:添加参数keep
可能效果更好:
df.a.nlargest(5,keep='first').min()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.