Skip to content

Commit 2a02b00

Browse files
authored
Update nlargest nsmallest doc (#56369)
* Update nlargest nsmallest doc * double line break * code review * code review for nsmallest * whitespace * whitespace
1 parent cf2710d commit 2a02b00

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

pandas/core/frame.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -7505,8 +7505,8 @@ def nlargest(
75057505
75067506
- ``first`` : prioritize the first occurrence(s)
75077507
- ``last`` : prioritize the last occurrence(s)
7508-
- ``all`` : do not drop any duplicates, even it means
7509-
selecting more than `n` items.
7508+
- ``all`` : keep all the ties of the smallest item even if it means
7509+
selecting more than ``n`` items.
75107510
75117511
Returns
75127512
-------
@@ -7568,7 +7568,9 @@ def nlargest(
75687568
Italy 59000000 1937894 IT
75697569
Brunei 434000 12128 BN
75707570
7571-
When using ``keep='all'``, all duplicate items are maintained:
7571+
When using ``keep='all'``, the number of element kept can go beyond ``n``
7572+
if there are duplicate values for the smallest element, all the
7573+
ties are kept:
75727574
75737575
>>> df.nlargest(3, 'population', keep='all')
75747576
population GDP alpha-2
@@ -7578,6 +7580,16 @@ def nlargest(
75787580
Maldives 434000 4520 MV
75797581
Brunei 434000 12128 BN
75807582
7583+
However, ``nlargest`` does not keep ``n`` distinct largest elements:
7584+
7585+
>>> df.nlargest(5, 'population', keep='all')
7586+
population GDP alpha-2
7587+
France 65000000 2583560 FR
7588+
Italy 59000000 1937894 IT
7589+
Malta 434000 12011 MT
7590+
Maldives 434000 4520 MV
7591+
Brunei 434000 12128 BN
7592+
75817593
To order by the largest values in column "population" and then "GDP",
75827594
we can specify multiple columns like in the next example.
75837595
@@ -7614,8 +7626,8 @@ def nsmallest(
76147626
76157627
- ``first`` : take the first occurrence.
76167628
- ``last`` : take the last occurrence.
7617-
- ``all`` : do not drop any duplicates, even it means
7618-
selecting more than `n` items.
7629+
- ``all`` : keep all the ties of the largest item even if it means
7630+
selecting more than ``n`` items.
76197631
76207632
Returns
76217633
-------
@@ -7669,7 +7681,9 @@ def nsmallest(
76697681
Tuvalu 11300 38 TV
76707682
Nauru 337000 182 NR
76717683
7672-
When using ``keep='all'``, all duplicate items are maintained:
7684+
When using ``keep='all'``, the number of element kept can go beyond ``n``
7685+
if there are duplicate values for the largest element, all the
7686+
ties are kept.
76737687
76747688
>>> df.nsmallest(3, 'population', keep='all')
76757689
population GDP alpha-2
@@ -7678,6 +7692,16 @@ def nsmallest(
76787692
Iceland 337000 17036 IS
76797693
Nauru 337000 182 NR
76807694
7695+
However, ``nsmallest`` does not keep ``n`` distinct
7696+
smallest elements:
7697+
7698+
>>> df.nsmallest(4, 'population', keep='all')
7699+
population GDP alpha-2
7700+
Tuvalu 11300 38 TV
7701+
Anguilla 11300 311 AI
7702+
Iceland 337000 17036 IS
7703+
Nauru 337000 182 NR
7704+
76817705
To order by the smallest values in column "population" and then "GDP", we can
76827706
specify multiple columns like in the next example.
76837707

0 commit comments

Comments
 (0)