Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.nsmallest#

Series.nsmallest(n=5,keep='first')[source]#

Return the smallestn elements.

Parameters:
nint, default 5

Return this many ascending sorted values.

keep{‘first’, ‘last’, ‘all’}, default ‘first’

When there are duplicate values that cannot all fit in aSeries ofn elements:

  • first : return the firstn occurrences in orderof appearance.

  • last : return the lastn occurrences in reverseorder of appearance.

  • all : keep all occurrences. This can result in a Series ofsize larger thann.

Returns:
Series

Then smallest values in the Series, sorted in increasing order.

See also

Series.nlargest

Get then largest elements.

Series.sort_values

Sort Series by values.

Series.head

Return the firstn rows.

Notes

Faster than.sort_values().head(n) for smalln relative tothe size of theSeries object.

Examples

>>>countries_population={"Italy":59000000,"France":65000000,..."Brunei":434000,"Malta":434000,..."Maldives":434000,"Iceland":337000,..."Nauru":11300,"Tuvalu":11300,..."Anguilla":11300,"Montserrat":5200}>>>s=pd.Series(countries_population)>>>sItaly       59000000France      65000000Brunei        434000Malta         434000Maldives      434000Iceland       337000Nauru          11300Tuvalu         11300Anguilla       11300Montserrat      5200dtype: int64

Then smallest elements wheren=5 by default.

>>>s.nsmallest()Montserrat    5200Nauru        11300Tuvalu       11300Anguilla     11300Iceland     337000dtype: int64

Then smallest elements wheren=3. Defaultkeep value is‘first’ so Nauru and Tuvalu will be kept.

>>>s.nsmallest(3)Montserrat   5200Nauru       11300Tuvalu      11300dtype: int64

Then smallest elements wheren=3 and keeping the lastduplicates. Anguilla and Tuvalu will be kept since they are the lastwith value 11300 based on the index order.

>>>s.nsmallest(3,keep='last')Montserrat   5200Anguilla    11300Tuvalu      11300dtype: int64

Then smallest elements wheren=3 with all duplicates kept. Notethat the returned Series has four elements due to the three duplicates.

>>>s.nsmallest(3,keep='all')Montserrat   5200Nauru       11300Tuvalu      11300Anguilla    11300dtype: int64

[8]ページ先頭

©2009-2025 Movatter.jp