Skip to content
Advertisement

filter for rows with n largest values for each group

Context

I want, for each team, the rows of the data frame that contains the top three scoring players.

In my head, it is a combination of Dataframe.nlargest() and Dataframe.groupby() but I don’t think this is supported. My ideal solution is:

  • performed directly on df without having to create other dataframes
  • legible, and
  • relatively performant (real df shape is 7M rows and 5 col)

Input

JavaScript
JavaScript

Desired Output

JavaScript
JavaScript

Advertisement

Answer

You can use df.groupby.rank method:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement