maxframe.dataframe.Series.nlargest#
- Series.nlargest(n, keep='first')#
返回最大的 n 个元素。
- 参数:
n (int, default 5) -- 返回此数量的降序排列的值。
keep ({'first', 'last', 'all'}, default 'first') -- 当存在重复值且无法全部放入 n 个元素的 Series 中时: -
first: 按出现顺序返回前 n 个。 -last: 按出现顺序的逆序返回后 n 个。 -all: 保留所有出现的情况。这可能导致 Series 的大小超过 n。
- 返回:
Series 中最大的 n 个值,按降序排列。
- 返回类型:
参见
Series.nsmallest获取最小的 n 个元素。
Series.sort_values按值对 Series 排序。
Series.head返回前 n 行。
备注
对于较小的 n 相对于
Series对象的大小,此方法比.sort_values(ascending=False).head(n)更快。示例
>>> import maxframe.dataframe as md >>> countries_population = {"Italy": 59000000, "France": 65000000, ... "Malta": 434000, "Maldives": 434000, ... "Brunei": 434000, "Iceland": 337000, ... "Nauru": 11300, "Tuvalu": 11300, ... "Anguilla": 11300, "Montserrat": 5200} >>> s = md.Series(countries_population) >>> s.execute() Italy 59000000 France 65000000 Malta 434000 Maldives 434000 Brunei 434000 Iceland 337000 Nauru 11300 Tuvalu 11300 Anguilla 11300 Montserrat 5200 dtype: int64
默认情况下
n=5时的最大的 n 个元素。>>> s.nlargest().execute() France 65000000 Italy 59000000 Malta 434000 Maldives 434000 Brunei 434000 dtype: int64
n=3时的最大的 n 个元素。默认的 keep 值为 'first',因此 Malta 会被保留。>>> s.nlargest(3).execute() France 65000000 Italy 59000000 Malta 434000 dtype: int64
n 个最大元素,其中
n=3并保留最后的重复值。Brunei 将被保留,因为根据索引顺序它是值为 434000 的最后一个。>>> s.nlargest(3, keep='last').execute() France 65000000 Italy 59000000 Brunei 434000 dtype: int64
n 个最大元素,其中
n=3并保留所有重复值。注意由于三个重复值,返回的 Series 有五个元素。>>> s.nlargest(3, keep='all').execute() France 65000000 Italy 59000000 Malta 434000 Maldives 434000 Brunei 434000 dtype: int64