[Python] How can I change the shape of the dataframe to have two headers when I have...

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

  1. Stack

    Stack Membro Participativo

    this is my df:

    symbol year_bin metric value row
    0 USA500.IDX 2025-1 total_trades 32.00 0
    1 GBPUSD 2025-1 total_trades 11.00 0
    2 GBPUSD 2025-1 total_trades 14.00 1
    3 AUDUSD 2025-1 total_trades 5.00 0
    4 AUDUSD 2025-1 total_trades 43.00 1
    5 USA500.IDX 2025-1 win_rate 31.25 0
    6 GBPUSD 2025-1 win_rate 27.27 0
    7 GBPUSD 2025-1 win_rate 21.43 1
    8 AUDUSD 2025-1 win_rate 0.00 0
    9 AUDUSD 2025-1 win_rate 25.58 1


    I wantto change the shape to this

    2025-1


    symbol total_trades win_rate
    Usa500.idx 32 31.25 . . .


    this is what I have tried

    df_pivot = df_melted.pivot(
    index=['symbol', 'year_bin'],
    columns='metric',
    values='value'
    )

    df_pivot = df_pivot.reset_index()


    but it gives me an error:

    ValueError: Index contains duplicate entries, cannot reshape

    Continue reading...

Compartilhe esta Página