Skip to content

Commit 4181042

Browse files
authored
DOC: Update documentation DataFrame.nsmallest (#31833)
1 parent 3060972 commit 4181042

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

pandas/core/frame.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -5222,7 +5222,7 @@ def nsmallest(self, n, columns, keep="first") -> "DataFrame":
52225222
Examples
52235223
--------
52245224
>>> df = pd.DataFrame({'population': [59000000, 65000000, 434000,
5225-
... 434000, 434000, 337000, 11300,
5225+
... 434000, 434000, 337000, 337000,
52265226
... 11300, 11300],
52275227
... 'GDP': [1937894, 2583560 , 12011, 4520, 12128,
52285228
... 17036, 182, 38, 311],
@@ -5239,43 +5239,44 @@ def nsmallest(self, n, columns, keep="first") -> "DataFrame":
52395239
Maldives 434000 4520 MV
52405240
Brunei 434000 12128 BN
52415241
Iceland 337000 17036 IS
5242-
Nauru 11300 182 NR
5242+
Nauru 337000 182 NR
52435243
Tuvalu 11300 38 TV
52445244
Anguilla 11300 311 AI
52455245
52465246
In the following example, we will use ``nsmallest`` to select the
5247-
three rows having the smallest values in column "a".
5247+
three rows having the smallest values in column "population".
52485248
52495249
>>> df.nsmallest(3, 'population')
5250-
population GDP alpha-2
5251-
Nauru 11300 182 NR
5252-
Tuvalu 11300 38 TV
5253-
Anguilla 11300 311 AI
5250+
population GDP alpha-2
5251+
Tuvalu 11300 38 TV
5252+
Anguilla 11300 311 AI
5253+
Iceland 337000 17036 IS
52545254
52555255
When using ``keep='last'``, ties are resolved in reverse order:
52565256
52575257
>>> df.nsmallest(3, 'population', keep='last')
52585258
population GDP alpha-2
52595259
Anguilla 11300 311 AI
52605260
Tuvalu 11300 38 TV
5261-
Nauru 11300 182 NR
5261+
Nauru 337000 182 NR
52625262
52635263
When using ``keep='all'``, all duplicate items are maintained:
52645264
52655265
>>> df.nsmallest(3, 'population', keep='all')
5266-
population GDP alpha-2
5267-
Nauru 11300 182 NR
5268-
Tuvalu 11300 38 TV
5269-
Anguilla 11300 311 AI
5266+
population GDP alpha-2
5267+
Tuvalu 11300 38 TV
5268+
Anguilla 11300 311 AI
5269+
Iceland 337000 17036 IS
5270+
Nauru 337000 182 NR
52705271
5271-
To order by the largest values in column "a" and then "c", we can
5272+
To order by the smallest values in column "population" and then "GDP", we can
52725273
specify multiple columns like in the next example.
52735274
52745275
>>> df.nsmallest(3, ['population', 'GDP'])
52755276
population GDP alpha-2
52765277
Tuvalu 11300 38 TV
5277-
Nauru 11300 182 NR
52785278
Anguilla 11300 311 AI
5279+
Nauru 337000 182 NR
52795280
"""
52805281
return algorithms.SelectNFrame(
52815282
self, n=n, keep=keep, columns=columns

0 commit comments

Comments
 (0)