Inconsistent results between 'nlargest' and 'sort_values' function · Issue #19563 · pandas-dev/pandas (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@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.