maxframe.dataframe.Series.nsmallest#
- Series.nsmallest(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.nlargest获取最大的 n 个元素。
Series.sort_values按值对 Series 进行排序。
Series.head返回前 n 行。
备注
对于相对于
Series对象大小较小的 n,速度比.sort_values().head(n)更快。示例
>>> import maxframe.dataframe as md >>> countries_population = {"Italy": 59000000, "France": 65000000, ... "Brunei": 434000, "Malta": 434000, ... "Maldives": 434000, "Iceland": 337000, ... "Nauru": 11300, "Tuvalu": 11300, ... "Anguilla": 11300, "Montserrat": 5200} >>> s = md.Series(countries_population) >>> s.execute() Italy 59000000 France 65000000 Brunei 434000 Malta 434000 Maldives 434000 Iceland 337000 Nauru 11300 Tuvalu 11300 Anguilla 11300 Montserrat 5200 dtype: int64
默认情况下
n=5时的最小的 n 个元素。>>> s.nsmallest().execute() Montserrat 5200 Nauru 11300 Tuvalu 11300 Anguilla 11300 Iceland 337000 dtype: int64
n=3时的最小的 n 个元素。默认的 keep 值为 'first',因此 Nauru 和 Tuvalu 会被保留。>>> s.nsmallest(3).execute() Montserrat 5200 Nauru 11300 Tuvalu 11300 dtype: int64
n 个最小元素,其中
n=3并保留最后的重复项。安圭拉和图瓦卢将被保留,因为它们在索引顺序中是值为 11300 的最后两个。>>> s.nsmallest(3, keep='last').execute() Montserrat 5200 Anguilla 11300 Tuvalu 11300 dtype: int64
n 个最小元素,其中
n=3并保留所有重复项。注意返回的 Series 有四个元素,因为有三个重复项。>>> s.nsmallest(3, keep='all').execute() Montserrat 5200 Nauru 11300 Tuvalu 11300 Anguilla 11300 dtype: int64