[Python] Iterate over a Dataframe column and find value based on condition

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

  1. Stack

    Stack Membro Participativo

    I have a Dataframe with 2 columns. I want to compare the value of first column with some threshold for 5 iterations and if it exceeds that value, check the corresponding value of the another column

    DF: In the below example I need to check at what value of 'Inst' where 'Error' was less than 2.5 for next consecutive 5 iterations

    Expected result: Inst was 204273 204302

    Below is what I tried but it did not work. Any pointers or better way of implementing it would be nice

    count = 0
    for i in range(len(df["Inst"])):
    while count < 6:
    if df["Error"] < 2.5:
    count += 1
    continue
    result = df["Inst"][i-5]


    Below is my DF:

    Error Inst
    0 2.595795 204267
    1 2.568556 204268
    2 2.562618 204269
    4 2.538956 204271
    5 2.520247 204272
    6 2.498345 204273 #
    7 2.474890 204274
    8 2.467736 204275
    9 2.471115 204276
    10 2.466424 204280
    11 2.495388 204284
    12 2.520301 204285
    13 2.604358 204291
    14 2.553243 204299
    15 2.490774 204302 #
    16 2.452384 204303
    17 2.434171 204304
    18 2.404764 204305
    19 2.388775 204306
    20 2.384337 204307

    Continue reading...

Compartilhe esta Página