question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataFrame.nlargest result error

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': [1, 10, 8, 11, 8],
    'b': list('abdce'),
    'c': [1.0, 2.0, np.nan, 3.0, 4.0]})
print('_________')
print(df.nlargest(10,['a','b']))

Problem description

DataFrame的nlargest在遇到rank相同的情况时,结果错误。如下,第二行和第四行反复出现了。

Expected Output

    a  b    c
3  11  c  3.0
1  10  b  2.0
2   8  d  NaN
4   8  e  4.0
2   8  d  NaN
4   8  e  4.0
0   1  a  1.0
[Finished in 0.6s]

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jrebackcommented, May 12, 2017

@flystarhe your question is not clear

These might be what you want

In [5]: df.groupby(['a', 'b']).nth([0, 1, 2])
Out[5]: 
        c
a  b     
1  a  1.0
8  d  NaN
   e  4.0
10 b  2.0
11 c  3.0

In [6]: df.sort_values(['a', 'b']).groupby(['a', 'b']).head(10)
Out[6]: 
    a  b    c
0   1  a  1.0
2   8  d  NaN
4   8  e  4.0
1  10  b  2.0
3  11  c  3.0

0reactions
nasirudeenraheemcommented, Nov 5, 2019

better still since it does deals string format. so the df[‘’].value_counts().nlargest

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python pandas nlargest() not working properly with keep = 'all'
Hi Chrispresso, I am trying to find the top3 result, but I want it to not drop duplicates. So, for example, if there...
Read more >
pandas.DataFrame.nlargest — pandas 1.5.2 documentation
Return the first n rows with the largest values in columns , in descending order. The columns that are not specified are returned...
Read more >
Pandas DataFrame nlargest() Method - Finxter
DataFrame nlargest (). The nlargest() method retrieves and returns the first n (number) of rows containing the largest column values in descending order....
Read more >
Python | Pandas DataFrame.nlargest() - GeeksforGeeks
Pandas nlargest () method is used to get n largest values from a data frame or a series. Syntax: DataFrame.nlargest(n, columns, keep='first').
Read more >
Become a pandas ninja with nlargest(), nsmallest(), query and ...
The result DataFrame in this case is ordered in ascending order based on columns we specify. In this section, we talk about nlargest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found