[Python] Pandas group by - maximum row for a subset

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with weekly product sales

    product_id week_number sales
    A1 1 1000
    A1 2 2000
    A1 3 3000
    A2 1 8000
    A2 2 4000
    A2 3 2000

    I want to add a column that identifies rows where the total sales were the highest for the given product:

    product_id week_number sales product_max
    A1 1 1000 FALSE
    A1 2 2000 FALSE
    A1 3 3000 TRUE
    A2 1 8000 TRUE
    A2 2 4000 FALSE
    A2 3 2000 FALSE

    Since A1 had its highest sales on week 3, that row is tagged as True. But week 3 wasn't the highest for A2. (And so for A2, week 1 is tagged as True.)

    I know I could write a loop to do this and cycle through each of the product IDs one by one, but I am wondering if there is a way to do this with a different function - possibly Pandas Groupby?

    Thank you!

    Continue reading...

Compartilhe esta Página