Table Of Contents

Search

Enter search terms or a module, class or function name.

pandas.DataFrame.nsmallest

DataFrame.nsmallest(n, columns, keep='first')[source]

Get the rows of a DataFrame sorted by the n smallest values of columns.

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 : do not drop any duplicates, even it means selecting more than n items.

New in version 0.24.0.

Returns:
DataFrame

Examples

>>> df = pd.DataFrame({'a': [1, 10, 8, 11, 8, 2],
...                    'b': list('abdcef'),
...                    'c': [1.0, 2.0, np.nan, 3.0, 4.0, 9.0]})
>>> df
    a  b    c
0   1  a  1.0
1  10  b  2.0
2   8  d  NaN
3  11  c  3.0
4   8  e  4.0
5   2  f  9.0

In the following example, we will use nsmallest to select the three rows having the smallest values in column “a”.

>>> df.nsmallest(3, 'a')
   a  b    c
0  1  a  1.0
5  2  f  9.0
2  8  d  NaN

When using keep='last', ties are resolved in reverse order:

>>> df.nsmallest(3, 'a', keep='last')
   a  b    c
0  1  a  1.0
5  2  f  9.0
4  8  e  4.0

When using keep='all', all duplicate items are maintained:

>>> df.nsmallest(3, 'a', keep='all')
   a  b    c
0  1  a  1.0
5  2  f  9.0
2  8  d  NaN
4  8  e  4.0

To order by the largest values in column “a” and then “c”, we can specify multiple columns like in the next example.

>>> df.nsmallest(3, ['a', 'c'])
   a  b    c
0  1  a  1.0
5  2  f  9.0
4  8  e  4.0

Attempting to use nsmallest on non-numeric dtypes will raise a TypeError:

>>> df.nsmallest(3, 'b')

Traceback (most recent call last): TypeError: Column ‘b’ has dtype object, cannot use method ‘nsmallest’

Scroll To Top