[Python] Finding the max or min of values in local sets of rows of a Pandas Dataframe

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    So for example, I have a dataframe like this

    Value Placement
    0 12 high
    1 15 high
    2 18 high
    3 14 high
    4 4 low
    5 5 low
    6 9 high
    7 11 high
    8 2 low
    9 1 low
    10 3 low
    11 2 low


    I want to create a second dataframe that contains the the highest value in the "Value" column for each set of consecutive rows with "high" placement, and the lowest value in the "Value" column for each set of consecutive rows with "low" placement. So something like

    Value Placement
    0 18 high
    1 4 low
    2 11 high
    3 1 low


    I also don't want to change the order of the rows, as the order of the "highs" and "lows" is critical to the functionality of the project.

    I could just iterate through the original dataframe and keep track of the the numbers in "Value" until a change in "Placement" is detected, but I've heard dataframe iteration is very slow and should be avoided if possible. Is there some way to do this without iteration? TIA

    Continue reading...

Compartilhe esta Página