diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e0efa93379bca..817c71cb814d6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5230,7 +5230,7 @@ def nsmallest(self, n, columns, keep="first") -> "DataFrame": Examples -------- >>> df = pd.DataFrame({'population': [59000000, 65000000, 434000, - ... 434000, 434000, 337000, 11300, + ... 434000, 434000, 337000, 337000, ... 11300, 11300], ... 'GDP': [1937894, 2583560 , 12011, 4520, 12128, ... 17036, 182, 38, 311], @@ -5247,18 +5247,18 @@ def nsmallest(self, n, columns, keep="first") -> "DataFrame": Maldives 434000 4520 MV Brunei 434000 12128 BN Iceland 337000 17036 IS - Nauru 11300 182 NR + Nauru 337000 182 NR Tuvalu 11300 38 TV Anguilla 11300 311 AI In the following example, we will use ``nsmallest`` to select the - three rows having the smallest values in column "a". + three rows having the smallest values in column "population". >>> df.nsmallest(3, 'population') - population GDP alpha-2 - Nauru 11300 182 NR - Tuvalu 11300 38 TV - Anguilla 11300 311 AI + population GDP alpha-2 + Tuvalu 11300 38 TV + Anguilla 11300 311 AI + Iceland 337000 17036 IS When using ``keep='last'``, ties are resolved in reverse order: @@ -5266,24 +5266,25 @@ def nsmallest(self, n, columns, keep="first") -> "DataFrame": population GDP alpha-2 Anguilla 11300 311 AI Tuvalu 11300 38 TV - Nauru 11300 182 NR + Nauru 337000 182 NR When using ``keep='all'``, all duplicate items are maintained: >>> df.nsmallest(3, 'population', keep='all') - population GDP alpha-2 - Nauru 11300 182 NR - Tuvalu 11300 38 TV - Anguilla 11300 311 AI + population GDP alpha-2 + Tuvalu 11300 38 TV + Anguilla 11300 311 AI + Iceland 337000 17036 IS + Nauru 337000 182 NR - To order by the largest values in column "a" and then "c", we can + To order by the smallest values in column "population" and then "GDP", we can specify multiple columns like in the next example. >>> df.nsmallest(3, ['population', 'GDP']) population GDP alpha-2 Tuvalu 11300 38 TV - Nauru 11300 182 NR Anguilla 11300 311 AI + Nauru 337000 182 NR """ return algorithms.SelectNFrame( self, n=n, keep=keep, columns=columns