modin.pandas.DataFrame.nsmallest¶
- DataFrame.nsmallest(n, columns, keep='first') DataFrame[source]¶
Return the first n rows ordered by columns in ascending order.
Return the first n rows with the smallest values in columns, in ascending order. The columns that are not specified are returned as well, but not used for ordering.
This method is equivalent to
df.sort_values(columns, ascending=True).head(n)- Parameters:
n (int) – Number of items to retrieve.
columns (list or str) – Column name or names to order by.
keep ({'first', 'last', 'all'}, default 'first') –
Where there are duplicate values:
first: take the first occurrence.last: take the last occurrence.all: keep all the ties of the largest item even if it means selecting more thannitems.
- Return type:
See also
DataFrame.nlargestReturn the first n rows ordered by columns in descending order.
DataFrame.sort_valuesSort DataFrame by the values.
DataFrame.headReturn the first n rows without re-ordering.
Examples
In the following example, we will use
nsmallestto select the three rows having the smallest values in column “population”.When using
keep='last', ties are resolved in reverse order:When using
keep='all', the number of element kept can go beyondn. if there are duplicate values for the largest element, all the ties are kept.However,
nsmallestdoes not keepndistinct smallest elements:To order by the smallest values in column “population” and then “GDP”, we can specify multiple columns like in the next example.