[Python] Transforming Data with implicit categories in header with pandas

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

  1. Stack

    Stack Membro Participativo

    I have a table like:

    | 2022 | 2022 | 2021 | 2021
    class | A | B | A | B
    -----------|------|------|------|------
    X | 1 | 2 | 3 | 4
    Y | 5 | 6 | 7 | 8


    How can I transform it to following form?

    year | category | class | value
    ----------------------------------
    2022 | A | X | 1
    2022 | A | Y | 5
    2022 | B | X | 2
    2022 | B | Y | 6
    2021 | A | X | 3
    2021 | A | Y | 7
    2021 | B | X | 4
    2021 | B | Y | 8


    I tried various combinations of pd.melt with no success..

    Thx in advance!

    Continue reading...

Compartilhe esta Página