Skip to content

How to find the index of max/min? #292

Answered by jheer
nachocab asked this question in Q&A
Discussion options

You must be logged in to vote

One way to do this is to use filter to reduce the table to just the max (or min) values. Note that a minimum or maximum value may appear more than once. If desired, you can then use another filter to limit the result to a single max/min per group.

aq.table({
    g: [1, 1, 1, 1, 2, 2, 2, 2],
    v: [1, 1, 2, 2, 3, 3, 4, 4],
    y: [1, 2, 3, 4, 1, 2, 3, 4]
  })
  .groupby('g')
  .filter(d => op.max(d.v) === d.v)   // remove rows that do not contain max(v)
  .filter(d => op.row_number() === 1) // retain only the first row in each group
  .view()

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@nachocab
Comment options

@jheer
Comment options

jheer Sep 1, 2022
Maintainer

@nachocab
Comment options

@jheer
Comment options

jheer Sep 1, 2022
Maintainer

Answer selected by jheer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants