bigframes.pandas.DataFrame.nsmallest#
- DataFrame.nsmallest(n: int, columns: Hashable | Sequence[Hashable], keep: str = '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), but more performant.Note
This function cannot be used with all column types. For example, when specifying columns with object or category dtypes,
TypeErroris raised.Examples:
>>> import bigframes.pandas as bpd >>> df = bpd.DataFrame({"A": [1, 1, 3, 3, 5, 5], ... "B": [5, 6, 3, 4, 1, 2], ... "C": ['a', 'b', 'a', 'b', 'a', 'b']}) >>> df A B C 0 1 5 a 1 1 6 b 2 3 3 a 3 3 4 b 4 5 1 a 5 5 2 b [6 rows x 3 columns]
Returns rows with the smallest value in ‘A’, including all ties:
>>> df.nsmallest(1, 'A', keep = "all") A B C 0 1 5 a 1 1 6 b [2 rows x 3 columns]
Returns the first row with the smallest value in ‘A’, default behavior in case of ties:
>>> df.nsmallest(1, 'A') A B C 0 1 5 a [1 rows x 3 columns]
Returns the last row with the smallest value in ‘A’ in case of ties:
>>> df.nsmallest(1, 'A', keep = "last") A B C 1 1 6 b [1 rows x 3 columns]
Returns rows with the smallest values in ‘A’ and ‘C’
>>> df.nsmallest(1, ['A', 'C']) A B C 0 1 5 a [1 rows x 3 columns]
- Parameters:
n (int) – Number of rows to return.
columns (label or list of labels) – Column label(s) to order by.
keep ({'first', 'last', 'all'}, default 'first') –
Where there are duplicate values:
first: prioritize the first occurrence(s)last: prioritize the last occurrence(s)all: do not drop any duplicates, even it means selecting more than n items.
- Returns:
The first n rows ordered by the given columns in ascending order.
- Return type:
- Raises:
ValueError – If value of
keepis notfirst,last, orall.