you have a pandas dataframe with three columns, filled with state names, city names and arbitrary numbers respectively. How to retrieve top 2 cities per state. (top according to the max number in the third column)

Retrieve top 2 cities per state based on max number in third column of pandas dataframe.Group the dataframe by state columnSort each group by the third column in descending orderRetrieve the top 2 row...
see more
Paras Chaudhary

df.groupby('state')['city', 'number'].nlargest(2, 'number')

Add answer anonymously...
Affine Data Scientist Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
55 L+


5 L+


2 Cr+


1 Cr+


Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2023 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Hello, Guest
Contribute to help millions!
Write a review
Share interview
Contribute salary
Add office photos
Add office benefits