@@ -3840,6 +3840,81 @@ def nsorted(
3840
3840
ascending : bool ,
3841
3841
keep : NsmallestNlargestKeep = "first" ,
3842
3842
) -> Series :
3843
+ """
3844
+ Return the top `n` elements, sorted in ascending or descending order.
3845
+
3846
+ Parameters
3847
+ ----------
3848
+ n : int, default 5
3849
+ Return this many descending sorted values.
3850
+ ascending : bool
3851
+ If True, return the smallest `n` elements. If False, return
3852
+ largest `n` elements.
3853
+ keep : {'first', 'last', 'all'}, default 'first'
3854
+ When there are duplicate values that cannot all fit in a
3855
+ Series of `n` elements:
3856
+
3857
+ - ``first`` : return the first `n` occurrences in order
3858
+ of appearance.
3859
+ - ``last`` : return the last `n` occurrences in reverse
3860
+ order of appearance.
3861
+ - ``all`` : keep all occurrences. This can result in a Series of
3862
+ size larger than `n`.
3863
+
3864
+ Returns
3865
+ -------
3866
+ Series
3867
+ The `n` top values in the Series.
3868
+
3869
+ See Also
3870
+ --------
3871
+ Series.nsmallest: Get the `n` smallest elements.
3872
+ Series.nlargest: Get the `n` largest elements.
3873
+
3874
+ Examples
3875
+ --------
3876
+ >>> countries_population = {
3877
+ ... "Italy": 59000000,
3878
+ ... "France": 65000000,
3879
+ ... "Malta": 434000,
3880
+ ... "Maldives": 434000,
3881
+ ... "Brunei": 434000,
3882
+ ... "Iceland": 337000,
3883
+ ... "Nauru": 11300,
3884
+ ... "Tuvalu": 11300,
3885
+ ... "Anguilla": 11300,
3886
+ ... "Montserrat": 5200,
3887
+ ... }
3888
+ >>> s = pd.Series(countries_population)
3889
+ >>> s
3890
+ Italy 59000000
3891
+ France 65000000
3892
+ Malta 434000
3893
+ Maldives 434000
3894
+ Brunei 434000
3895
+ Iceland 337000
3896
+ Nauru 11300
3897
+ Tuvalu 11300
3898
+ Anguilla 11300
3899
+ Montserrat 5200
3900
+ dtype: int64
3901
+
3902
+ Get the `n` largest elements.
3903
+
3904
+ >>> s.nsorted(n=3, ascending=False)
3905
+ France 65000000
3906
+ Italy 59000000
3907
+ Malta 434000
3908
+ dtype: int64
3909
+
3910
+ Get the `n` smallest elements.
3911
+
3912
+ >>> s.nsorted(n=3, ascending=True)
3913
+ Montserrat 5200
3914
+ Nauru 11300
3915
+ Tuvalu 11300
3916
+ dtype: int64
3917
+ """
3843
3918
return selectn .SelectNSeries (
3844
3919
self ,
3845
3920
n = n ,
0 commit comments