[Python] Merging multiple rows of data based Parent Child relationship

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

  1. Stack

    Stack Membro Participativo

    I would like to know the code to merge cells which has a parent and child relationship based on dash lines "-" as condition.

    The relationship

    1. Parent "A" will not have any dash lines
    2. Immediate Child "B" will have a a single dash line "A - B"
    3. "C" is the Child of "B" , will have two dash lines "A - B -- C"

    Dash lines decide the parent child relationship. There is a single space between dash line and the parent and child i.e A - B -- C

    Please see the below table. I would like build an column named MERGED DATA from the ORIGINAL DATA column .

    Please note the "Powdered Cheese" will find the parent as "Milk" and not "Fish" .

    SL No ORIGINAL DATA MERGED DATA
    1 FISH FISH
    2 - Salmon FISH - Salmon
    3 -- Trout FISH - Salmon -- Trout
    4 Milk Milk
    5 - Milk Powder Milk - Milk Powder
    6 - Yoghurt Milk - Yoghurt
    7 - Cheese Milk - Cheese
    8 -- Powdered Cheese Milk - Cheese -- Powdered Cheese

    Continue reading...

Compartilhe esta Página