[Python] How to transpose date from column to row with mapping value in python?

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python pandas dataframe like this:


    id | Client | Type | V1 | V2 | V3 | V4 |
    ---------------------------------------------
    0 | John | Time | 20 | 24 | | |
    0 | John |Score | 7 | 8 | | |
    0 | John |Level | A | B | | |
    1 | Tim | Time | 15 | 19 | 20 | 18 |
    1 | Tim |Score | 9 | 8 | 6 | 9 |
    1 | Tim |Level | C | A | C | A |
    2 | Daniel | Time | 17 | | | |
    2 | Daniel |Score | 7 | | | |
    2 | Daniel |Level | B | | | |​

    I would like to transpose into this table:

    Client | Time |Score | Level
    --------------------------------
    John | 20 | 7 | A |
    John | 24 | 8 | B |
    Tim | 15 | 9 | C |
    Tim | 19 | 8 | A |
    Tim | 20 | 6 | C |
    Tim | 18 | 9 | A |
    Daniel | 17 | 7 | B |


    I read some posts using pd.melt or df.stack but it seems my case is a bit different. Any advice is appreciated.

    Continue reading...

Compartilhe esta Página