pythonbeginner

Pandas nlargest / nsmallest

Efficiently retrieve the N largest or smallest rows without sorting the full DataFrame.

python
import pandas as pd
import numpy as np

df = pd.DataFrame({'product':[f'P{i}' for i in range(1000)],'revenue': np.random.randint(100,10000,1000),'orders': np.random.randint(1,500,1000)})

top5 = df.nlargest(5, 'revenue')
bottom5 = df.nsmallest(5, 'revenue')

print('Top 5 by revenue:')
print(top5[['product','revenue']])
print('Bottom 5:')
print(bottom5[['product','revenue']])

Use Cases

  • top-N queries
  • leaderboards
  • anomaly detection

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.