[Python] Conditionally slice a pandas dataframe based on a start and a stop value and...

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to a slice (extract a subset from a) huge dataframe based on a start and a stop value (strictly speaking, not a between possible range) of a speed, and it has to be monotonically increasing.

    I am looking for the range to be strictly bounded by the start and stop values. The extracted range has to start with a start value and end with the stop value, and has to be monotonically increasing.

    Here is an example:

    import pandas as pd
    import numpy as np

    speed = [0,1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,14,13,11,12,15,12,14,16,11,10,9,5,12,20,21,22,25,27,32,34,35,30,20]
    df = pd.DataFrame(speed,columns = 'speed')


    I want to only extract the slice where the speed starts for example at 20 and stops at 35, I don't care afterwards, all I care about is to extract this slice with a set start and end value point, and it has to be monotonically increasing.

    The typical result for the example would look like this :

    Result

    20
    21
    22
    25
    27
    32
    34
    35


    found this answer with the XOR ^ operator and a cummax, but adding the monotocity condition like is_monotnoic() or diff() >=0 messes things up and the reset_index with loc idea mentioned in the same post does not seem to work as well.

    It seems trivial to do it with pandas, but could not find a sufficient answer.

    Thanks in advance

    Continue reading...

Compartilhe esta Página