pandas 1.4.2

NotesParametersReturnsBackRef
nsmallest(self, n: 'int' = 5, keep: 'str' = 'first') -> 'Series'

Notes

Faster than .sort_values().head(n) for small n relative to the size of the Series object.

Parameters

n : int, default 5

Return this many ascending sorted values.

keep : {'first', 'last', 'all'}, default 'first'

When there are duplicate values that cannot all fit in a Series of n elements:

Returns

Series

The n smallest values in the Series, sorted in increasing order.

Return the smallest n elements.

See Also

Series.head

Return the first :None:None:`n` rows.

Series.nlargest

Get the :None:None:`n` largest elements.

Series.sort_values

Sort Series by values.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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)
... s Italy 59000000 France 65000000 Brunei 434000 Malta 434000 Maldives 434000 Iceland 337000 Nauru 11300 Tuvalu 11300 Anguilla 11300 Montserrat 5200 dtype: int64

The n smallest elements where n=5 by default.

This example is valid syntax, but we were not able to check execution
>>> s.nsmallest()
Montserrat    5200
Nauru        11300
Tuvalu       11300
Anguilla     11300
Iceland     337000
dtype: int64

The n smallest elements where n=3 . Default :None:None:`keep` value is 'first' so Nauru and Tuvalu will be kept.

This example is valid syntax, but we were not able to check execution
>>> s.nsmallest(3)
Montserrat   5200
Nauru       11300
Tuvalu      11300
dtype: int64

The n smallest elements where n=3 and keeping the last duplicates. Anguilla and Tuvalu will be kept since they are the last with value 11300 based on the index order.

This example is valid syntax, but we were not able to check execution
>>> s.nsmallest(3, keep='last')
Montserrat   5200
Anguilla    11300
Tuvalu      11300
dtype: int64

The n smallest elements where n=3 with all duplicates kept. Note that the returned Series has four elements due to the three duplicates.

This example is valid syntax, but we were not able to check execution
>>> s.nsmallest(3, keep='all')
Montserrat   5200
Nauru       11300
Tuvalu      11300
Anguilla    11300
dtype: int64
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.groupby.generic.SeriesGroupBy.nlargest pandas.core.series.Series.nlargest

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#3838
type: <class 'function'>
Commit: