[Python] How to turn rows in each group in dataframe into columns?

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe:

    col1 col2 col3 val col4
    a1 b1 c1 10 dd
    a1 b1 c1 15 kk
    a2 b2 c2 20 ff
    a2 b2 c2 35 mm
    a3 b3 c3 9 sd


    I want to put each value in column "val" from each group col1, col2, col3 into each column. So desired result is:

    col1 col2 col3 val_1 col4_1 val_2 col4_2
    a1 b1 c1 10 dd 15 kk
    a2 b2 c2 20 ff 35 mm
    a3 b3 c3 9 sd NA NA


    How to do that? Is there any function to turn those rows into columns within group?

    Continue reading...

Compartilhe esta Página