Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.nsmallest#

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

Return the firstn rows ordered bycolumns in ascending order.

Return the firstn rows with the smallest values incolumns, inascending order. The columns that are not specified are returned aswell, but not used for ordering.

This method is equivalent todf.sort_values(columns,ascending=True).head(n), but moreperformant.

Parameters:
nint

Number of items to retrieve.

columnslist 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 meansselecting more thann items.

Returns:
DataFrame

See also

DataFrame.nlargest

Return the firstn rows ordered bycolumns in descending order.

DataFrame.sort_values

Sort DataFrame by the values.

DataFrame.head

Return the firstn rows without re-ordering.

Examples

>>>df=pd.DataFrame({'population':[59000000,65000000,434000,...434000,434000,337000,337000,...11300,11300],...'GDP':[1937894,2583560,12011,4520,12128,...17036,182,38,311],...'alpha-2':["IT","FR","MT","MV","BN",..."IS","NR","TV","AI"]},...index=["Italy","France","Malta",..."Maldives","Brunei","Iceland",..."Nauru","Tuvalu","Anguilla"])>>>df          population      GDP alpha-2Italy       59000000  1937894      ITFrance      65000000  2583560      FRMalta         434000    12011      MTMaldives      434000     4520      MVBrunei        434000    12128      BNIceland       337000    17036      ISNauru         337000      182      NRTuvalu         11300       38      TVAnguilla       11300      311      AI

In the following example, we will usensmallest to select thethree rows having the smallest values in column “population”.

>>>df.nsmallest(3,'population')          population    GDP alpha-2Tuvalu         11300     38      TVAnguilla       11300    311      AIIceland       337000  17036      IS

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

>>>df.nsmallest(3,'population',keep='last')          population  GDP alpha-2Anguilla       11300  311      AITuvalu         11300   38      TVNauru         337000  182      NR

When usingkeep='all', the number of element kept can go beyondnif there are duplicate values for the largest element, all theties are kept.

>>>df.nsmallest(3,'population',keep='all')          population    GDP alpha-2Tuvalu         11300     38      TVAnguilla       11300    311      AIIceland       337000  17036      ISNauru         337000    182      NR

However,nsmallest does not keepn distinctsmallest elements:

>>>df.nsmallest(4,'population',keep='all')          population    GDP alpha-2Tuvalu         11300     38      TVAnguilla       11300    311      AIIceland       337000  17036      ISNauru         337000    182      NR

To order by the smallest values in column “population” and then “GDP”, we canspecify multiple columns like in the next example.

>>>df.nsmallest(3,['population','GDP'])          population  GDP alpha-2Tuvalu         11300   38      TVAnguilla       11300  311      AINauru         337000  182      NR

[8]ページ先頭

©2009-2025 Movatter.jp