Skip to content

Inconsistent results between 'nlargest' and 'sort_values' function #19563

Closed
@ZhuBaohe

Description

@ZhuBaohe
df = pd.DataFrame({'a': [-2, -1, 1, 10, 8, 11, -1],
       'b': list('abdceff'),
       'c': [1.0, 2.0, 4.0, 3.2, np.nan, 3.0, 4.0]})    

df
Out[316]: 
    a  b    c
0  -2  a  1.0
1  -1  b  2.0
2   1  d  4.0
3  10  c  3.2
4   8  e  NaN
5  11  f  3.0
6  -1  f  4.0

df.nlargest(5, ['a', 'c'])
Out[317]: 
    a  b    c
6  -1  f  4.0
5  11  f  3.0
3  10  c  3.2
4   8  e  NaN
2   1  d  4.0

df.sort_values(by=['a','c'], ascending=False).head(5)
Out[318]: 
    a  b    c
5  11  f  3.0
3  10  c  3.2
4   8  e  NaN
2   1  d  4.0
6  -1  f  4.0

I think their results should be the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions