<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
df=pd.DataFrame({'variable': ['a','a','b','b','c','c'],
                 'value' : [1000,2000,10,20,100,200]},
                 index=[1,2]*3)
df.nlargest(3,'value')
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-s1">df</span><span class="pl-c1">=</span><span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'variable'</span>: [<span class="pl-s">'a'</span>,<span class="pl-s">'a'</span>,<span class="pl-s">'b'</span>,<span class="pl-s">'b'</span>,<span class="pl-s">'c'</span>,<span class="pl-s">'c'</span>],
                 <span class="pl-s">'value'</span> : [<span class="pl-c1">1000</span>,<span class="pl-c1">2000</span>,<span class="pl-c1">10</span>,<span class="pl-c1">20</span>,<span class="pl-c1">100</span>,<span class="pl-c1">200</span>]},
                 <span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>]<span class="pl-c1">*</span><span class="pl-c1">3</span>)
<span class="pl-s1">df</span>.<span class="pl-en">nlargest</span>(<span class="pl-c1">3</span>,<span class="pl-s">'value'</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">The result should only have 3 rows.  If the index has unique values, it is correct.  But when the index has duplicate values, the incorrect result is produced.</p>
<p dir="auto">The example produces the following output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="   value variable
2   2000        a
2   2000        a
1   1000        a
2    200        c
2    200        c
1    100        c
2     20        b
2     20        b
1     10        b"><pre class="notranslate"><code class="notranslate">   value variable
2   2000        a
2   2000        a
1   1000        a
2    200        c
2    200        c
1    100        c
2     20        b
2     20        b
1     10        b
</code></pre></div>
<h4 dir="auto">Expected Output</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="   value variable
2  2000       a
1  1000       a
2  200        c"><pre class="notranslate"><code class="notranslate">   value variable
2  2000       a
1  1000       a
2  200        c
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
<p dir="auto">pandas: 0.19.0<br>
nose: None<br>
pip: 8.1.2<br>
setuptools: 27.2.0<br>
Cython: None<br>
numpy: 1.11.2<br>
scipy: None<br>
statsmodels: None<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.5.3<br>
pytz: 2016.7<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: None<br>
openpyxl: None<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: 4.5.1<br>
html5lib: 0.999<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">The function below has been incorrectly implemented. If the frame has an index with duplicated values, you will get a result with more than <code class="notranslate">n</code> rows and not properly sorted. So <code class="notranslate">nsmallest</code> and <code class="notranslate">nlargest</code> for DataFrame doesn't return a correct frame in this particular case.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def _nsorted(self, columns, n, method, keep):
    if not com.is_list_like(columns):
        columns = [columns]
    columns = list(columns)
    ser = getattr(self[columns[0]], method)(n, keep=keep)
    ascending = dict(nlargest=False, nsmallest=True)[method]
    return self.loc[ser.index].sort_values(columns, ascending=ascending,
                                           kind='mergesort')"><pre class="notranslate"><code class="notranslate">def _nsorted(self, columns, n, method, keep):
    if not com.is_list_like(columns):
        columns = [columns]
    columns = list(columns)
    ser = getattr(self[columns[0]], method)(n, keep=keep)
    ascending = dict(nlargest=False, nsmallest=True)[method]
    return self.loc[ser.index].sort_values(columns, ascending=ascending,
                                           kind='mergesort')
</code></pre></div>