[Python] fill row value based on column identifier

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe like below, and would like to fill value from previous row value based on id field, so, any record with 4 in colA get what the previous colA=3 records' colC value, colB stays the same.

    I've tried ffill, and shift, but couldn't get what I want, please help. Thank you.

    colA colB colC
    3 air 00JTHYU1
    4 ground 00JTHYU0
    4 ground 00JTHYU0
    4 ground 00JTHYU0
    3 air 00JTHYU4
    4 ground 00JTHYU0
    4 air 00JTHYU0
    3 ground 00JTHYU7
    4 ground 00JTHYU0

    Result:

    colA colB colC
    3 air 00JTHYU1
    4 ground 00JTHYU1
    4 ground 00JTHYU1
    4 ground 00JTHYU1
    3 air 00JTHYU4
    4 ground 00JTHYU4
    4 air 00JTHYU4
    3 ground 00JTHYU7
    4 ground 00JTHYU7

    Continue reading...

Compartilhe esta Página